public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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>
mernen (author)
Sun Dec 21 14:38:12 -0800 2008
lifo (committer)
Sun Dec 21 15:24:06 -0800 2008
commit  f7bd0beb67c5d9d50e37aa596605b91e61197fbe
tree    19cadf3a53bc48920c7f3fc6e5dd9c8f7e10f4fc
parent  389534c38c3baaa63ce5cc2ba3bd169415419167
...
1494
1495
1496
1497
1498
 
 
 
1499
1500
1501
 
 
 
 
 
1502
1503
1504
...
1494
1495
1496
 
 
1497
1498
1499
1500
1501
 
1502
1503
1504
1505
1506
1507
1508
1509
0
@@ -1494,11 +1494,16 @@ module ActiveRecord #:nodoc:
0
           end
0
 
0
           if scoped?(:find, :order)
0
-            scoped_order = reverse_sql_order(scope(:find, :order))
0
-            scoped_methods.select { |s| s[:find].update(:order => scoped_order) }
0
+            scope = scope(:find)
0
+            original_scoped_order = scope[:order]
0
+            scope[:order] = reverse_sql_order(original_scoped_order)
0
           end
0
 
0
-          find_initial(options.merge({ :order => order }))
0
+          begin
0
+            find_initial(options.merge({ :order => order }))
0
+          ensure
0
+            scope[:order] = original_scoped_order if original_scoped_order
0
+          end
0
         end
0
 
0
         def reverse_sql_order(order_query)
...
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
0
@@ -27,6 +27,24 @@ class MethodScopingTest < ActiveRecord::TestCase
0
     end
0
   end
0
 
0
+  def test_scoped_find_last
0
+    highest_salary = Developer.find(:first, :order => "salary DESC")
0
+
0
+    Developer.with_scope(:find => { :order => "salary" }) do
0
+      assert_equal highest_salary, Developer.last
0
+    end
0
+  end
0
+
0
+  def test_scoped_find_last_preserves_scope
0
+    lowest_salary = Developer.find(:first, :order => "salary ASC")
0
+    highest_salary = Developer.find(:first, :order => "salary DESC")
0
+
0
+    Developer.with_scope(:find => { :order => "salary" }) do
0
+      assert_equal highest_salary, Developer.last
0
+      assert_equal lowest_salary, Developer.first
0
+    end
0
+  end
0
+
0
   def test_scoped_find_combines_conditions
0
     Developer.with_scope(:find => { :conditions => "salary = 9000" }) do
0
       assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")

Comments