lundi 21 mars 2016

deleting from ruby array based on SQL query

I have a controller action that looks like this

def list
  @jobs = users_jobs_list_data
end

This is getting data from another action, user_jobs_list_data, which for simplicity's sake looks like this...

def users_jobs_list_data
  user_jobs_query = current_account.user_jobs).select("
    company_locations.name AS company_location_name")
    .joins("LEFT JOIN company_locations ON users.company_location_id = company_locations.id")

    ActiveRecord::Base.connection.exec_query(users_jobs_query.to_sql)
end

so the @jobs variable ends up coming in as an array rather than an activerecord relation. Here's where I am stuck. I am being passed a parameter which defines that only current records be shown. so now the action looks like this

def list
  if params[:current] == 'true'
    << show reduced results here >>
  else
    @jobs = users_jobs_list_data
  end
end

So the final piece is to reduce @jobs based on the criteria for current. This is where I am stuck. This is the code to access the current records

@jobs.where("'#{Date.today.iso8601}' between start_date and ifnull(end_date, '#{(Date.today + 1).iso8601}')")

This works fine on @jobs if @jobs is an activerecord relation, but not in this case, where @jobs is an array because I cannot call where on it. How can I resolve this bearing in mind @jobs really needs to stay as an array?

Aucun commentaire:

Enregistrer un commentaire