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_last (find(:last)) affects other calls on the same scope

#1499

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.

Reported by Daniel Luz · December 1st, 2008 @ 01:26 AM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Daniel Luz
    Daniel Luz
    • 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.

    December 1st, 2008 @ 04:36 AM

  2. Daniel Luz
    Daniel Luz
    • 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

    December 1st, 2008 @ 05:59 AM

  3. Chris Kampmeier
    Chris Kampmeier

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

    December 1st, 2008 @ 07:20 AM

  4. Daniel Luz
    Daniel Luz

    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.

    December 1st, 2008 @ 08:05 AM

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

    December 21st, 2008 @ 07:04 PM

  6. Repository
    Repository
    • State changed from new to resolved

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

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

    December 22nd, 2008 @ 12:14 AM