Dennis Krupenik
2012-Sep-17 01:45 UTC
Duplicate queries while eager loading multiple associations
Hello all, given the following models and console log: https://gist.github.com/3735116 I do not understand why the latter statements (o.order_items and o.total) yield sql queries. Have I missed something in AR documentation? What should be done to get rid of redundant sql queries? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/tMZF4jbjC7oJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Sep-17 07:13 UTC
Re: Duplicate queries while eager loading multiple associations
On Monday, September 17, 2012 2:45:05 AM UTC+1, Dennis Krupenik wrote:> > Hello all, > > given the following models and console log: > https://gist.github.com/3735116 > > I do not understand why the latter statements (o.order_items and o.total) > yield sql queries. > Have I missed something in AR documentation? What should be done to get > rid of redundant sql queries? > > oi = o.order_items.includes(item: :photos)does not mutate o.order_items, adding the includes to it: it returns a new array of objects that has the extra options applied. You do o = Order.includes(order_items: {item: photos}).last in which case o.order_items should not execute any queries Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/MFvhLesbj-kJ. For more options, visit https://groups.google.com/groups/opt_out.
Dennis Krupenik
2012-Sep-17 14:18 UTC
Re: Duplicate queries while eager loading multiple associations
thanks a lot, that did it -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dITnC-NFoywJ. For more options, visit https://groups.google.com/groups/opt_out.