This project is archived and is in readonly mode.

#2740 ✓stale
Matt Hanlon

Find conditions get clobbered by nested scopes

Reported by Matt Hanlon | May 29th, 2009 @ 09:52 PM | in 3.x

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


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


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:

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.

Comments and changes to this ticket

  • Matt Hanlon

    Matt Hanlon May 29th, 2009 @ 09:57 PM

    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


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

  • Joe Goldberg

    Joe Goldberg May 29th, 2009 @ 10:18 PM

    I'm having this problem too. How can we get to see this ticket so he can comment on the change?

  • Matt Hanlon

    Matt Hanlon June 22nd, 2009 @ 07:54 PM

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

    Matt Hanlon June 22nd, 2009 @ 07:54 PM

    • Tag changed from 2.3.2, activerecord, condition, find, named_scope, s to 2.3.2, activerecord, condition, find, named_scope
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Rohit Arondekar

    Rohit Arondekar October 9th, 2010 @ 04:06 AM

    • 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.

  • bingbing
  • Henning

    Henning March 31st, 2011 @ 04:45 PM

    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:

    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?

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>