Skip to content
This repository
Browse code

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
Daniel Luz authored December 21, 2008 lifo committed December 21, 2008
11  activerecord/lib/active_record/base.rb
@@ -1494,11 +1494,16 @@ def find_last(options)
1494 1494
           end
1495 1495
 
1496 1496
           if scoped?(:find, :order)
1497  
-            scoped_order = reverse_sql_order(scope(:find, :order))
1498  
-            scoped_methods.select { |s| s[:find].update(:order => scoped_order) }
  1497
+            scope = scope(:find)
  1498
+            original_scoped_order = scope[:order]
  1499
+            scope[:order] = reverse_sql_order(original_scoped_order)
1499 1500
           end
1500 1501
 
1501  
-          find_initial(options.merge({ :order => order }))
  1502
+          begin
  1503
+            find_initial(options.merge({ :order => order }))
  1504
+          ensure
  1505
+            scope[:order] = original_scoped_order if original_scoped_order
  1506
+          end
1502 1507
         end
1503 1508
 
1504 1509
         def reverse_sql_order(order_query)
18  activerecord/test/cases/method_scoping_test.rb
@@ -27,6 +27,24 @@ def test_scoped_find_first
27 27
     end
28 28
   end
29 29
 
  30
+  def test_scoped_find_last
  31
+    highest_salary = Developer.find(:first, :order => "salary DESC")
  32
+
  33
+    Developer.with_scope(:find => { :order => "salary" }) do
  34
+      assert_equal highest_salary, Developer.last
  35
+    end
  36
+  end
  37
+
  38
+  def test_scoped_find_last_preserves_scope
  39
+    lowest_salary = Developer.find(:first, :order => "salary ASC")
  40
+    highest_salary = Developer.find(:first, :order => "salary DESC")
  41
+
  42
+    Developer.with_scope(:find => { :order => "salary" }) do
  43
+      assert_equal highest_salary, Developer.last
  44
+      assert_equal lowest_salary, Developer.first
  45
+    end
  46
+  end
  47
+
30 48
   def test_scoped_find_combines_conditions
31 49
     Developer.with_scope(:find => { :conditions => "salary = 9000" }) do
32 50
       assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")

0 notes on commit f7bd0be

Please sign in to comment.
Something went wrong with that request. Please try again.