Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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...
commit f7bd0beb67c5d9d50e37aa596605b91e61197fbe 1 parent 389534c
@mernen mernen authored lifo committed
View
11 activerecord/lib/active_record/base.rb
@@ -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)
View
18 activerecord/test/cases/method_scoping_test.rb
@@ -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'")
Please sign in to comment.
Something went wrong with that request. Please try again.