I've been looking at this ticket: #643 named_scope with eager loading affects activerecord objects - Ruby on Rails - rails 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 people.first.friends...
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 end
Is preloading throwing away the scope when it does its thing going to cause trouble here?
Fred