aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorNeeraj Singh <neerajdotname@gmail.com>2010-09-01 12:04:29 -0400
committerSantiago Pastorino <santiago@wyeworks.com>2010-09-05 08:13:42 -0300
commit91fec0d24d50d3b3e90c48b2501ca913544781cc (patch)
treef3dd8ff6c1f9b1ca6f3029a827297904ce165e22 /activerecord
parentf9c21ce6580601109a6c4ac55c63bdea031f3203 (diff)
downloadrails-91fec0d24d50d3b3e90c48b2501ca913544781cc.tar.gz
rails-91fec0d24d50d3b3e90c48b2501ca913544781cc.tar.bz2
rails-91fec0d24d50d3b3e90c48b2501ca913544781cc.zip
order should always be concatenated.
order that is declared first has highest priority in all cases. Here are some examples. Car.order('name desc').find(:first, :order => 'id').name Car.named_scope_with_order.named_scope_with_another_order Car.order('id DESC').scoping do Car.find(:first, :order => 'id asc') end No special treatment to with_scope or scoping. Also note that if default_scope declares an order then the order declared in default_scope has the highest priority unless with_exclusive_scope is used. Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb3
-rw-r--r--activerecord/test/cases/base_test.rb8
-rw-r--r--activerecord/test/cases/relation_scoping_test.rb4
-rw-r--r--activerecord/test/cases/relations_test.rb18
-rw-r--r--activerecord/test/fixtures/cars.yml5
-rw-r--r--activerecord/test/models/car.rb5
6 files changed, 35 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 4b2c647a26..e3f1496b9a 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -99,9 +99,8 @@ module ActiveRecord
relation = relation.readonly(options[:readonly]) if options.key? :readonly
- # Give precedence to newly-applied orders and groups to play nicely with with_scope
[:group, :order].each do |finder|
- relation.send("#{finder}_values=", Array.wrap(options[finder]) + relation.send("#{finder}_values")) if options.has_key?(finder)
+ relation.send("#{finder}_values=", relation.send("#{finder}_values") + Array.wrap(options[finder])) if options.has_key?(finder)
end
relation = relation.where(options[:conditions]) if options.has_key?(:conditions)
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index d58e302cb2..07b06e15a3 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1139,18 +1139,18 @@ class BasicsTest < ActiveRecord::TestCase
scoped_developers = Developer.send(:with_scope, :find => { :limit => 1 }) do
Developer.find(:all, :order => 'salary DESC')
end
- # Test scope order + find order, find has priority
+ # Test scope order + find order, order has priority
scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :order => 'id DESC' }) do
Developer.find(:all, :order => 'salary ASC')
end
assert scoped_developers.include?(developers(:poor_jamis))
- assert scoped_developers.include?(developers(:david))
+ assert ! scoped_developers.include?(developers(:david))
assert ! scoped_developers.include?(developers(:jamis))
assert_equal 3, scoped_developers.size
# Test without scoped find conditions to ensure we get the right thing
- developers = Developer.find(:all, :order => 'id', :limit => 1)
- assert scoped_developers.include?(developers(:david))
+ assert ! scoped_developers.include?(Developer.find(1))
+ assert scoped_developers.include?(Developer.find(11))
end
def test_scoped_find_limit_offset_including_has_many_association
diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb
index cdfd62a675..e27bb65144 100644
--- a/activerecord/test/cases/relation_scoping_test.rb
+++ b/activerecord/test/cases/relation_scoping_test.rb
@@ -398,8 +398,8 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal expected, received
end
- def test_overwriting_default_scope
- expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary }
+ def test_order_in_default_scope_should_prevail
+ expected = Developer.find(:all, :order => 'salary desc').collect { |dev| dev.salary }
received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
assert_equal expected, received
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index be038bfa74..e21e1cb635 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -667,4 +667,22 @@ class RelationTest < ActiveRecord::TestCase
def test_relations_limit_with_conditions_or_limit
assert_equal Post.limit(2).size, Post.limit(2).all.size
end
+
+ def test_order_with_find_with_order
+ assert_equal 'zyke', Car.order('name desc').find(:first, :order => 'id').name
+ end
+
+ def test_default_scope_order_with_named_scope_order
+ assert_equal 'zyke', Car.order_using_new_style.limit(1).first.name
+ assert_equal 'zyke', Car.order_using_old_style.limit(1).first.name
+ end
+
+ def test_order_using_scoping
+ car = Car.order('id DESC').scoping do
+ Car.find(:first, :order => 'id asc')
+ end
+ assert_equal 'zyke', car.name
+ end
+
+
end
diff --git a/activerecord/test/fixtures/cars.yml b/activerecord/test/fixtures/cars.yml
index 23c98e8144..b4c748aaa7 100644
--- a/activerecord/test/fixtures/cars.yml
+++ b/activerecord/test/fixtures/cars.yml
@@ -2,3 +2,8 @@ honda:
id: 1
name: honda
engines_count: 0
+
+zyke:
+ id: 2
+ name: zyke
+ engines_count: 0
diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb
index 903ec53288..a1e351b54e 100644
--- a/activerecord/test/models/car.rb
+++ b/activerecord/test/models/car.rb
@@ -7,4 +7,9 @@ class Car < ActiveRecord::Base
scope :incl_tyres, includes(:tyres)
scope :incl_engines, includes(:engines)
+ default_scope :order => 'name desc'
+
+ scope :order_using_new_style, order('name asc')
+ scope :order_using_old_style, :order => 'name asc'
+
end