Re: Duplicate queries while eager loading multiple associations

15 views
Skip to first unread message

Frederick Cheung

unread,
Sep 17, 2012, 3:13:26 AM9/17/12
to rubyonra...@googlegroups.com


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

Dennis Krupenik

unread,
Sep 17, 2012, 10:18:22 AM9/17/12
to rubyonra...@googlegroups.com
thanks a lot, that did it
Reply all
Reply to author
Forward
0 new messages