This project is archived and is in readonly mode.

#1499 ✓resolved
Daniel Luz

find_last (find(:last)) affects other calls on the same scope

Reported by Daniel Luz | December 1st, 2008 @ 01:26 AM | in 2.x

Using find(:last) within a scope leaves side effects on it, affecting all future calls within the same scope.

Pseudocode example:


Book.with_scope(:find => {:order => "id"}) do
  p Book.find(:all)  # returns: 1, 2, 3, 4, 5...
  p Book.find(:last) # returns Book #10
  p Book.find(:all)  # now returns 10, 9, 8, 7...
  p Book.find(:last) # now returns Book #1
end

An ActiveRecord testcase patch is included.

Comments and changes to this ticket

  • Daniel Luz

    Daniel Luz December 1st, 2008 @ 04:36 AM

    • Tag changed from 2.1-stable, 2.2-stable, :order, activerecord, bug, find_last, named_scope, scope, with_scope to 2.1-stable, 2.2-stable, :order, activerecord, bug, find_last, named_scope, patch, scope, with_scope

    Patch altering the way #find_last works with scoped_methods.

    Rather than updating all elements on the stack, update just the current scope, and restore its state afterward.

  • Daniel Luz

    Daniel Luz December 1st, 2008 @ 05:59 AM

    • Title changed from “find_last (find(:last)) affects other calls on the same scope” to “[PATCH] find_last (find(:last)) affects other calls on the same scope”
  • Chris Kampmeier

    Chris Kampmeier December 1st, 2008 @ 07:20 AM

    +1, nice catch.

    Tried it out, looks good. Tests are great; patch fixes the problem, although I don't know enough about method scoping to know if there's a better way to do it. This one looks fine, though.

    p.s. if you merge those into one patch formatted with git format-patch, it makes it way easier to apply (and commit on your behalf). Directions here. Otherwise, git doesn't know who wrote the patch, thinks it's an uncommitted change, etc.

  • Daniel Luz

    Daniel Luz December 1st, 2008 @ 08:05 AM

    Testcase and fix in a single patch.

    They were separated because at first I didn't have a solution, so I just came here to leave a report and a test case. After some more fiddling I had a better understanding of how the scoping works and came up with this patch.

  • Pratik

    Pratik December 21st, 2008 @ 07:04 PM

    • Assigned user set to “Pratik”
    • Title changed from “[PATCH] find_last (find(:last)) affects other calls on the same scope” to “find_last (find(:last)) affects other calls on the same scope”
  • Repository

    Repository December 22nd, 2008 @ 12:14 AM

    • State changed from “new” to “resolved”

    (from [f7bd0beb67c5d9d50e37aa596605b91e61197fbe]) Ensure Model#last doesn't affects order for another finders inside the same scope [#1499 state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages