Scoping problem with :include

Skip to first unread message

Frederick Cheung

Dec 18, 2008, 8:38:02 PM12/18/08
I've been looking at this ticket:
In a nutshell it's about scoping. If you run

Person.some_scope.find :all, :include => :friends

Then the find on Person happens inside some_scope, which is fine, but
if :friends has class_name Person (i.e. we have a self referential
association) then the query we make to load the friends will also be
scoped by some_scope, which is (I believe undesirable behaviour).

I believe that changing association_preload.rb to wrap its finds in
with_exclusive_scope solves the problem. That's what my patch does and
it fixes the failing test cases I wrote first.
Can anyone think of a case where this would be throwing away scoping
we didn't want to throw away? That wouldn't happen in the case above,
since were we not eager loading then we;d do

people = Person.some_scope.find :all

There's no scoping on the fetch of friends, so my patch brings the
eager loaded behaviour in line with the normal version.
But what if one was to do

Person.with_scope(...) do
Person.find :all, :include => :friends

Is preloading throwing away the scope when it does its thing going to
cause trouble here?


Reply all
Reply to author
0 new messages