Joseph Palermo
2009-Jul-27 18:48 UTC
2.3.3 bug in chained named scopes. Lambdas executing inside of the scope of the parent scopes.
In the process of upgrading from 2.2 to 2.3.3 we''ve come across a major change in named_scopes that seems to be a bug. The patch can be found here: https://rails.lighthouseapp.com/projects/8994/tickets/1267-methods-invoked-within-named_scope-procs-should-respect-the-scope-stack and it does do what it says, but we think that what it does is wrong. It leads to named_scopes that are order dependent, and queries inside of lambdas do not behave as you would expect. Given a User class with a ''friends'' association (pointing at other Users) with the following named_scopes: named_scope :named_bob, { :conditions => {:name => ''bob''} } named_scope :second_degree_friends, lambda{|user| user_friends = user.friends second_degree_friend_ids = user_friends.collect{|u| u.friend_ids} { :conditions => {:id => second_degree_friend_ids.flatten} } } User.named_bob.second_degree_friends(user_sam) User.second_degree_friends(user_sam).named_bob So one of these queries will only pull user_sam''s friends named ''bob'' and then see if any of them have friends named ''bob'' which is not what I want. The other one will work as expected where it finds all of user_sam''s second degree friends named bob.