Revert change on ActiveRecord::Relation#order method that prepends new

order on the old ones

The previous behavior added a major backward incompatibility since it
impossible to have a upgrade path without major changes on the
application code.

We are taking the most conservative path to be consistent with the idea
of having a smoother upgrade on Rails 4.

We are reverting the behavior for what was in Rails 3.x and,
if needed, we will implement a new API to prepend the order clauses in
Rails 4.1.
This commit is contained in:
Rafael Mendonça França 2013-07-29 23:18:33 -03:00
parent fbaae891ac
commit 92c5a2244e
8 changed files with 34 additions and 21 deletions

View File

@ -1,3 +1,18 @@
* Revert `ActiveRecord::Relation#order` change that make new order
prepend the old one.
Before:
User.order("name asc").order("created_at desc")
# SELECT * FROM users ORDER BY created_at desc, name asc
After:
User.order("name asc").order("created_at desc")
# SELECT * FROM users ORDER BY name asc, created_at desc
This also affects order defined in `default_scope` or any kind of associations.
* Don't allow `quote_value` to be called without a column. * Don't allow `quote_value` to be called without a column.
Some adapters require column information to do their job properly. Some adapters require column information to do their job properly.

View File

@ -299,7 +299,7 @@ module ActiveRecord
arg arg
} }
self.order_values = args.concat self.order_values self.order_values += args
self self
end end
@ -311,7 +311,7 @@ module ActiveRecord
# #
# User.order('email DESC').reorder('id ASC').order('name ASC') # User.order('email DESC').reorder('id ASC').order('name ASC')
# #
# generates a query with 'ORDER BY name ASC, id ASC'. # generates a query with 'ORDER BY id ASC, name ASC'.
def reorder(*args) def reorder(*args)
check_if_method_has_arguments!("reorder", args) check_if_method_has_arguments!("reorder", args)
spawn.reorder!(*args) spawn.reorder!(*args)

View File

@ -506,9 +506,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis') assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis')
end end
def test_find_should_prepend_to_association_order def test_find_should_append_to_association_order
ordered_developers = projects(:active_record).developers.order('projects.id') ordered_developers = projects(:active_record).developers.order('projects.id')
assert_equal ['projects.id', 'developers.name desc, developers.id desc'], ordered_developers.order_values assert_equal ['developers.name desc, developers.id desc', 'projects.id'], ordered_developers.order_values
end end
def test_dynamic_find_all_should_respect_readonly_access def test_dynamic_find_all_should_respect_readonly_access

View File

@ -253,9 +253,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size
end end
def test_find_should_prepend_to_association_order def test_find_should_append_to_association_order
ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id') ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id')
assert_equal ['companies.id', 'id DESC'], ordered_clients.order_values assert_equal ['id DESC', 'companies.id'], ordered_clients.order_values
end end
def test_dynamic_find_should_respect_association_order def test_dynamic_find_should_respect_association_order

View File

@ -180,7 +180,7 @@ class RelationTest < ActiveRecord::TestCase
end end
def test_finding_with_order_concatenated def test_finding_with_order_concatenated
topics = Topic.order('title').order('author_name') topics = Topic.order('author_name').order('title')
assert_equal 4, topics.to_a.size assert_equal 4, topics.to_a.size
assert_equal topics(:fourth).title, topics.first.title assert_equal topics(:fourth).title, topics.first.title
end end
@ -1183,20 +1183,20 @@ class RelationTest < ActiveRecord::TestCase
end end
def test_default_scope_order_with_scope_order def test_default_scope_order_with_scope_order
assert_equal 'honda', CoolCar.order_using_new_style.limit(1).first.name assert_equal 'zyke', CoolCar.order_using_new_style.limit(1).first.name
assert_equal 'honda', FastCar.order_using_new_style.limit(1).first.name assert_equal 'zyke', FastCar.order_using_new_style.limit(1).first.name
end end
def test_order_using_scoping def test_order_using_scoping
car1 = CoolCar.order('id DESC').scoping do car1 = CoolCar.order('id DESC').scoping do
CoolCar.all.merge!(:order => 'id asc').first CoolCar.all.merge!(order: 'id asc').first
end end
assert_equal 'honda', car1.name assert_equal 'zyke', car1.name
car2 = FastCar.order('id DESC').scoping do car2 = FastCar.order('id DESC').scoping do
FastCar.all.merge!(:order => 'id asc').first FastCar.all.merge!(order: 'id asc').first
end end
assert_equal 'honda', car2.name assert_equal 'zyke', car2.name
end end
def test_unscoped_block_style def test_unscoped_block_style

View File

@ -82,7 +82,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
end end
def test_scope_overwrites_default def test_scope_overwrites_default
expected = Developer.all.merge!(:order => ' name DESC, salary DESC').to_a.collect { |dev| dev.name } expected = Developer.all.merge!(order: 'salary DESC, name DESC').to_a.collect { |dev| dev.name }
received = DeveloperOrderedBySalary.by_name.to_a.collect { |dev| dev.name } received = DeveloperOrderedBySalary.by_name.to_a.collect { |dev| dev.name }
assert_equal expected, received assert_equal expected, received
end end
@ -94,7 +94,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
end end
def test_order_after_reorder_combines_orders def test_order_after_reorder_combines_orders
expected = Developer.order('id DESC, name DESC').collect { |dev| [dev.name, dev.id] } expected = Developer.order('name DESC, id DESC').collect { |dev| [dev.name, dev.id] }
received = Developer.order('name ASC').reorder('name DESC').order('id DESC').collect { |dev| [dev.name, dev.id] } received = Developer.order('name ASC').reorder('name DESC').order('id DESC').collect { |dev| [dev.name, dev.id] }
assert_equal expected, received assert_equal expected, received
end end
@ -253,8 +253,8 @@ class DefaultScopingTest < ActiveRecord::TestCase
end end
def test_order_in_default_scope_should_not_prevail def test_order_in_default_scope_should_not_prevail
expected = Developer.all.merge!(:order => 'salary').to_a.collect { |dev| dev.salary } expected = Developer.all.merge!(order: 'salary desc').to_a.collect { |dev| dev.salary }
received = DeveloperOrderedBySalary.all.merge!(:order => 'salary').to_a.collect { |dev| dev.salary } received = DeveloperOrderedBySalary.all.merge!(order: 'salary').to_a.collect { |dev| dev.salary }
assert_equal expected, received assert_equal expected, received
end end

View File

@ -549,11 +549,11 @@ Client.order("orders_count ASC, created_at DESC")
Client.order("orders_count ASC", "created_at DESC") Client.order("orders_count ASC", "created_at DESC")
``` ```
If you want to call `order` multiple times e.g. in different context, new order will prepend previous one If you want to call `order` multiple times e.g. in different context, new order will append previous one
```ruby ```ruby
Client.order("orders_count ASC").order("created_at DESC") Client.order("orders_count ASC").order("created_at DESC")
# SELECT * FROM clients ORDER BY created_at DESC, orders_count ASC # SELECT * FROM clients ORDER BY orders_count ASC, created_at DESC
``` ```
Selecting Specific Fields Selecting Specific Fields

View File

@ -153,8 +153,6 @@ Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must rep
* In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed. * In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed.
* Rails 4.0 has changed how orders get stacked in `ActiveRecord::Relation`. In previous versions of Rails, the new order was applied after the previously defined order. But this is no longer true. Check [Active Record Query guide](active_record_querying.html#ordering) for more information.
* Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. You shouldn't use instance methods since it's now deprecated. You should change them to use class methods, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`. * Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. You shouldn't use instance methods since it's now deprecated. You should change them to use class methods, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`.
* Rails 4.0 has removed `attr_accessible` and `attr_protected` feature in favor of Strong Parameters. You can use the [Protected Attributes gem](https://github.com/rails/protected_attributes) for a smooth upgrade path. * Rails 4.0 has removed `attr_accessible` and `attr_protected` feature in favor of Strong Parameters. You can use the [Protected Attributes gem](https://github.com/rails/protected_attributes) for a smooth upgrade path.