jeudi 26 mai 2016

has_many through with includes

This are the two models which i am using to query this

model 1
 has_many :locks_order_items, dependent: :destroy
 has_many :locks, through: :locks_order_items

model 2
 belongs_to :order_item
 belongs_to :lock

Form

<%= f.fields_for :order_items, @order_items do |f1| %>
   <% f1.object.locks_order_items.order("locks_order_items.check
   DESC, locks_order_items.id ASC").each do |m| 

   end %>
<% end %>

This is the query running, when load the form

SELECT "locks_order_items".* FROM "locks_order_items" WHERE 
"locks_order_items"."order_item_id" = 4444 ORDER BY 
locks_order_items.check DESC, locks_order_items.id ASC

if we have 10 entries in model 2, this above query, will run 10 times, i have tried to add includes, but still its running like that, is there any way to run that in one query with includes

Aucun commentaire:

Enregistrer un commentaire