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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to