ruby on rails - .includes with .order does not use single query -


with query:

cases = case.includes(:arrangement) 

i can loop through cases , arrangement values without more queries being generated every time call case.arrangement.id

with query:

cases = case.includes(:arrangement).order('arrangements.id asc') 

as loop through cases , arrangement values runs query load arrangement record case.

how can use .includes along ordering arrangement column , not have separate query run every time call case.arrangement.id?

here's how i'm looping after run query:

cases_array = [] cases.each_with_index |caseobj, i|   json_case = caseobj.as_json   json_case = json_case.merge(     :arrangement_id => caseobj.arrangement.try(:id),     :arrangement_location => caseobj.arrangement.try(:location).try(:name),     :arrangement_datetime => view_context.spell_date_and_time(       caseobj.arrangement.try(:timeslot).try(:timeslot)))   cases_array[i] = json_case   puts json_case end 

maybe should add more in include?

cases = case.includes(arrangement: [:location, :timeslot]).order('arrangements.id asc') 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -