Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Find conditions get clobbered by nested scopes

#2740

In ActiveRecord 2.3.2, nested scopes (named or dynamic) that have conditions hashes with common keys get overwritten by the final scope. For instance suppose there is a Widget model with id and name fields. Then

Widget.scoped_by_id([1,2,3]).scoped_by_name('foo').all

will return the widgets where the id is in [1,2,3] AND the name is 'foo'. One would then expect that

Widget.scoped_by_id([1,2,3]).scoped_by_id([3,4,5]).all

would return only the widget with id=3 (using AND), but in fact the id condition of the second scope clobbers the first and we get any widgets matching [3,4,5].

This behavior is a change from previous versions of ActiveRecord and seems to be an intentional result of this checkin:

http://github.com/rails/rails/commit/fe6ffce51dc16285094be49244b265...

To my mind this breaks the semantics of a scope for finding, which should be a way to restrict results and should be arbitrarily chainable.

Reported by Matt Hanlon · May 29th, 2009 @ 09:52 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Matt Hanlon
    Matt Hanlon

    As background, I came across this issue while upgrading a 2.1 rails app that uses nested find scopes to implement some user access control. Basically it uses named scopes like

    Asset.filter_by_user_access(some_user).filter_by_search_terms(some_term)
    

    In this case, the search scope clobbered the access scope since they both relied on some of the same fields.

    May 29th, 2009 @ 09:57 PM

  2. Joe Goldberg
    Joe Goldberg

    I'm having this problem too. How can we get http://github.com/lifo to see this ticket so he can comment on the change?

    May 29th, 2009 @ 10:18 PM

  3. Matt Hanlon
    Matt Hanlon
    • Tag set to 2.3.2, activerecord, condition, find, named_scope, s

    June 22nd, 2009 @ 07:54 PM

  4. Matt Hanlon
    Matt Hanlon
    • Tag changed from 2.3.2, activerecord, condition, find, named_scope, s to 2.3.2, activerecord, condition, find, named_scope

    June 22nd, 2009 @ 07:54 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 9th, 2010 @ 04:06 AM

  7. bingbing
  8. Henning
    Henning

    This issue seems to be fixed in Rails 3.0.5, but it is still a problem with older version of Rails 3 and every version of Rails 2. A long writeup on the issue can be found here: http://makandra.com/notes/740-know-the-side-effects-of-using-hashes...

    This should be fixed for Rails 2 because every application that uses scopes to restrict user access might be broken.

    I could provide a patch that fixes this for the 2-3-stable branch. Given that Rails 3 is the hot new thing now, would my fix still be considered for a future release?

    March 31st, 2011 @ 04:45 PM