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>
This commit is contained in:
Daniel Luz
2008-12-21 22:38:12 +00:00
committed by Pratik Naik
parent 389534c38c
commit f7bd0beb67
2 changed files with 26 additions and 3 deletions

View File

@@ -1494,11 +1494,16 @@ module ActiveRecord #:nodoc:
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)

View File

@@ -27,6 +27,24 @@ class MethodScopingTest < ActiveRecord::TestCase
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'")