Skip to content

Commit 609dae1

Browse files
committed
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. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/relation/query_methods.rb guides/source/upgrading_ruby_on_rails.md
1 parent d1ecd40 commit 609dae1

8 files changed

Lines changed: 35 additions & 20 deletions

File tree

‎activerecord/CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## unreleased ##
22

3+
* Revert `ActiveRecord::Relation#order` change that make new order
4+
prepend the old one.
5+
6+
Before:
7+
8+
User.order("name asc").order("created_at desc")
9+
# SELECT * FROM users ORDER BY created_at desc, name asc
10+
11+
After:
12+
13+
User.order("name asc").order("created_at desc")
14+
# SELECT * FROM users ORDER BY name asc, created_at desc
15+
16+
This also affects order defined in `default_scope` or any kind of associations.
17+
318
* When using optimistic locking, `update` was not passing the column to `quote_value`
419
to allow the connection adapter to properly determine how to quote the value. This was
520
affecting certain databases that use specific colmn types.

‎activerecord/lib/active_record/relation/query_methods.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def order!(*args) # :nodoc:
289289
arg.is_a?(Symbol) ? "#{quoted_table_name}.#{arg} ASC" : arg
290290
}
291291

292-
self.order_values = args + self.order_values
292+
self.order_values += args
293293
self
294294
end
295295

@@ -301,7 +301,7 @@ def order!(*args) # :nodoc:
301301
#
302302
# User.order('email DESC').reorder('id ASC').order('name ASC')
303303
#
304-
# generates a query with 'ORDER BY name ASC, id ASC'.
304+
# generates a query with 'ORDER BY id ASC, name ASC'.
305305
def reorder(*args)
306306
check_if_method_has_arguments!("reorder", args)
307307
spawn.reorder!(*args)

‎activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ def test_dynamic_find_should_respect_association_order
570570
assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis')
571571
end
572572

573-
def test_find_should_prepend_to_association_order
573+
def test_find_should_append_to_association_order
574574
ordered_developers = projects(:active_record).developers.order('projects.id')
575-
assert_equal ['projects.id', 'developers.name desc, developers.id desc'], ordered_developers.order_values
575+
assert_equal ['developers.name desc, developers.id desc', 'projects.id'], ordered_developers.order_values
576576
end
577577

578578
def test_dynamic_find_all_should_respect_readonly_access

‎activerecord/test/cases/associations/has_many_associations_test.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ def test_find_many_with_merged_options
308308
assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size
309309
end
310310

311-
def test_find_should_prepend_to_association_order
311+
def test_find_should_append_to_association_order
312312
ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id')
313-
assert_equal ['companies.id', 'id DESC'], ordered_clients.order_values
313+
assert_equal ['id DESC', 'companies.id'], ordered_clients.order_values
314314
end
315315

316316
def test_dynamic_find_should_respect_association_order

‎activerecord/test/cases/relations_test.rb‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_finding_last_with_arel_order
180180
end
181181

182182
def test_finding_with_order_concatenated
183-
topics = Topic.order('title').order('author_name')
183+
topics = Topic.order('author_name').order('title')
184184
assert_equal 4, topics.to_a.size
185185
assert_equal topics(:fourth).title, topics.first.title
186186
end
@@ -1174,20 +1174,20 @@ def test_order_by_relation_attribute
11741174
end
11751175

11761176
def test_default_scope_order_with_scope_order
1177-
assert_equal 'honda', CoolCar.order_using_new_style.limit(1).first.name
1178-
assert_equal 'honda', FastCar.order_using_new_style.limit(1).first.name
1177+
assert_equal 'zyke', CoolCar.order_using_new_style.limit(1).first.name
1178+
assert_equal 'zyke', FastCar.order_using_new_style.limit(1).first.name
11791179
end
11801180

11811181
def test_order_using_scoping
11821182
car1 = CoolCar.order('id DESC').scoping do
1183-
CoolCar.all.merge!(:order => 'id asc').first
1183+
CoolCar.all.merge!(order: 'id asc').first
11841184
end
1185-
assert_equal 'honda', car1.name
1185+
assert_equal 'zyke', car1.name
11861186

11871187
car2 = FastCar.order('id DESC').scoping do
1188-
FastCar.all.merge!(:order => 'id asc').first
1188+
FastCar.all.merge!(order: 'id asc').first
11891189
end
1190-
assert_equal 'honda', car2.name
1190+
assert_equal 'zyke', car2.name
11911191
end
11921192

11931193
def test_unscoped_block_style

‎activerecord/test/cases/scoping/default_scoping_test.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_default_scope_with_multiple_calls
7979
end
8080

8181
def test_scope_overwrites_default
82-
expected = Developer.all.merge!(:order => ' name DESC, salary DESC').to_a.collect { |dev| dev.name }
82+
expected = Developer.all.merge!(order: 'salary DESC, name DESC').to_a.collect { |dev| dev.name }
8383
received = DeveloperOrderedBySalary.by_name.to_a.collect { |dev| dev.name }
8484
assert_equal expected, received
8585
end
@@ -91,7 +91,7 @@ def test_reorder_overrides_default_scope_order
9191
end
9292

9393
def test_order_after_reorder_combines_orders
94-
expected = Developer.order('id DESC, name DESC').collect { |dev| [dev.name, dev.id] }
94+
expected = Developer.order('name DESC, id DESC').collect { |dev| [dev.name, dev.id] }
9595
received = Developer.order('name ASC').reorder('name DESC').order('id DESC').collect { |dev| [dev.name, dev.id] }
9696
assert_equal expected, received
9797
end
@@ -251,8 +251,8 @@ def test_unscope_errors_with_non_symbol_or_hash_arguments
251251
end
252252

253253
def test_order_in_default_scope_should_not_prevail
254-
expected = Developer.all.merge!(:order => 'salary').to_a.collect { |dev| dev.salary }
255-
received = DeveloperOrderedBySalary.all.merge!(:order => 'salary').to_a.collect { |dev| dev.salary }
254+
expected = Developer.all.merge!(order: 'salary desc').to_a.collect { |dev| dev.salary }
255+
received = DeveloperOrderedBySalary.all.merge!(order: 'salary').to_a.collect { |dev| dev.salary }
256256
assert_equal expected, received
257257
end
258258

‎guides/source/active_record_querying.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ Client.order("orders_count ASC, created_at DESC")
543543
Client.order("orders_count ASC", "created_at DESC")
544544
```
545545

546-
If you want to call `order` multiple times e.g. in different context, new order will prepend previous one
546+
If you want to call `order` multiple times e.g. in different context, new order will append previous one
547547

548548
```ruby
549549
Client.order("orders_count ASC").order("created_at DESC")
550-
# SELECT * FROM clients ORDER BY created_at DESC, orders_count ASC
550+
# SELECT * FROM clients ORDER BY orders_count ASC, created_at DESC
551551
```
552552

553553
Selecting Specific Fields

‎guides/source/upgrading_ruby_on_rails.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must rep
5353

5454
* The `delete` method in collection associations can now receive `Fixnum` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them.
5555

56-
* 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.
56+
* 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.
5757

5858
* 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`.
5959

0 commit comments

Comments
 (0)