Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure Model#last doesn't affects order for another finders inside th…
…e same scope [#1499 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
mernen authored and lifo committed Dec 21, 2008
1 parent 389534c commit f7bd0be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1494,11 +1494,16 @@ def find_last(options)
end

if scoped?(:find, :order)
scoped_order = reverse_sql_order(scope(:find, :order))
scoped_methods.select { |s| s[:find].update(:order => scoped_order) }
scope = scope(:find)
original_scoped_order = scope[:order]
scope[:order] = reverse_sql_order(original_scoped_order)
end

find_initial(options.merge({ :order => order }))
begin
find_initial(options.merge({ :order => order }))
ensure
scope[:order] = original_scoped_order if original_scoped_order
end
end

def reverse_sql_order(order_query)
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -27,6 +27,24 @@ def test_scoped_find_first
end
end

def test_scoped_find_last
highest_salary = Developer.find(:first, :order => "salary DESC")

Developer.with_scope(:find => { :order => "salary" }) do
assert_equal highest_salary, Developer.last
end
end

def test_scoped_find_last_preserves_scope
lowest_salary = Developer.find(:first, :order => "salary ASC")
highest_salary = Developer.find(:first, :order => "salary DESC")

Developer.with_scope(:find => { :order => "salary" }) do
assert_equal highest_salary, Developer.last
assert_equal lowest_salary, Developer.first
end
end

def test_scoped_find_combines_conditions
Developer.with_scope(:find => { :conditions => "salary = 9000" }) do
assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")
Expand Down

0 comments on commit f7bd0be

Please sign in to comment.