aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-08-01 06:24:14 +0900
committerGitHub <noreply@github.com>2018-08-01 06:24:14 +0900
commit57a91c4d4232b5a18f1514e82e7bc8d23254cfeb (patch)
tree246b2a905160b74911c91168ee2ab2f19cc73c85
parentb66bf91316d6726be359331bcaf436df66a9d257 (diff)
parent25760a492118886f5ecf570458217be8ea978678 (diff)
downloadrails-57a91c4d4232b5a18f1514e82e7bc8d23254cfeb.tar.gz
rails-57a91c4d4232b5a18f1514e82e7bc8d23254cfeb.tar.bz2
rails-57a91c4d4232b5a18f1514e82e7bc8d23254cfeb.zip
Merge pull request #33492 from kamipo/revert_breaking_default_order_contract
Revert the breaking existing default sort order contract
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb28
3 files changed, 14 insertions, 22 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 456f569718..d1ae41ab97 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,9 +1,3 @@
-* Don't impose primary key order if limit() has already been supplied.
-
- Fixes #23607
-
- *Brian Christian*
-
* Add environment & load_config dependency to `bin/rake db:seed` to enable
seed load in environments without Rails and custom DB configuration
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 93f3b67686..c5562c1ff0 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -550,7 +550,7 @@ module ActiveRecord
end
def ordered_relation
- if order_values.empty? && primary_key && limit_value.blank?
+ if order_values.empty? && primary_key
order(arel_attribute(primary_key).asc)
else
self
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 2f256dd36a..e73c88dd5d 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -463,6 +463,7 @@ class FinderTest < ActiveRecord::TestCase
expected = topics(:first)
expected.touch # PostgreSQL changes the default order if no order clause is used
assert_equal expected, Topic.first
+ assert_equal expected, Topic.limit(5).first
end
def test_model_class_responds_to_first_bang
@@ -485,6 +486,7 @@ class FinderTest < ActiveRecord::TestCase
expected = topics(:second)
expected.touch # PostgreSQL changes the default order if no order clause is used
assert_equal expected, Topic.second
+ assert_equal expected, Topic.limit(5).second
end
def test_model_class_responds_to_second_bang
@@ -507,6 +509,7 @@ class FinderTest < ActiveRecord::TestCase
expected = topics(:third)
expected.touch # PostgreSQL changes the default order if no order clause is used
assert_equal expected, Topic.third
+ assert_equal expected, Topic.limit(5).third
end
def test_model_class_responds_to_third_bang
@@ -529,6 +532,7 @@ class FinderTest < ActiveRecord::TestCase
expected = topics(:fourth)
expected.touch # PostgreSQL changes the default order if no order clause is used
assert_equal expected, Topic.fourth
+ assert_equal expected, Topic.limit(5).fourth
end
def test_model_class_responds_to_fourth_bang
@@ -551,6 +555,7 @@ class FinderTest < ActiveRecord::TestCase
expected = topics(:fifth)
expected.touch # PostgreSQL changes the default order if no order clause is used
assert_equal expected, Topic.fifth
+ assert_equal expected, Topic.limit(5).fifth
end
def test_model_class_responds_to_fifth_bang
@@ -713,6 +718,14 @@ class FinderTest < ActiveRecord::TestCase
assert_equal comments.limit(2).to_a.first(3), comments.limit(2).first(3)
end
+ def test_first_have_determined_order_by_default
+ expected = [companies(:second_client), companies(:another_client)]
+ clients = Client.where(name: expected.map(&:name))
+
+ assert_equal expected, clients.first(2)
+ assert_equal expected, clients.limit(5).first(2)
+ end
+
def test_take_and_first_and_last_with_integer_should_return_an_array
assert_kind_of Array, Topic.take(5)
assert_kind_of Array, Topic.first(5)
@@ -1265,21 +1278,6 @@ class FinderTest < ActiveRecord::TestCase
end
end
- def test_first_and_last_with_limit_for_order_without_primary_key
- # While Topic.first should impose an ordering by primary key,
- # Topic.limit(n).first should not
-
- Topic.first.touch # PostgreSQL changes the default order if no order clause is used
-
- assert_equal Topic.limit(1).to_a.first, Topic.limit(1).first
- assert_equal Topic.limit(2).to_a.first, Topic.limit(2).first
- assert_equal Topic.limit(2).to_a.first(2), Topic.limit(2).first(2)
-
- assert_equal Topic.limit(1).to_a.last, Topic.limit(1).last
- assert_equal Topic.limit(2).to_a.last, Topic.limit(2).last
- assert_equal Topic.limit(2).to_a.last(2), Topic.limit(2).last(2)
- end
-
def test_finder_with_offset_string
assert_nothing_raised { Topic.offset("3").to_a }
end