eager load bug?

25 views
Skip to first unread message

Robert Pankowecki

unread,
Feb 15, 2011, 5:07:16 AM2/15/11
to rubyonra...@googlegroups.com
o = Organization.first
o.people.includes(:addresses).each do |p|
puts p
puts p.addresses
end # 2 queries are executed, normal and for eager loading addresses

but

o.people.loaded? => false

Is that a bug or by-design behavior that o.people.includes(:address)
won't store the fetched records in o.people association ?

Robert Pankowecki

Ernie Miller

unread,
Feb 15, 2011, 6:49:53 AM2/15/11
to rubyonra...@googlegroups.com
On Feb 15, 2011, at 5:07 AM, Robert Pankowecki
<robert.p...@gmail.com> wrote:

That's by design. When you chain a scope off an associationproxy, it's
basically syntactic sugar for constructing your query the long way, by
doing Person.where(:organization_id => o.id)[...]

In the case you gave, above, the people association never needed to
load as the new scope was created, and to store the result of the
query you eventually executed in that association would be the wrong
thing to do -- it may have had additional conditions limiting the
results that would not fully load the association.

Inside your block, though, p.addresses.loaded? will be true, which is
what you asked for.

Hope this helps!

Reply all
Reply to author
Forward
0 new messages