1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #24131 from brchristian/limit_and_primary_key

ActiveRecord: limit() and primary_key
This commit is contained in:
Richard Schneeman 2018-07-21 07:49:34 -05:00 committed by GitHub
commit d162188dd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View file

@ -1,3 +1,9 @@
* 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

View file

@ -550,7 +550,7 @@ module ActiveRecord
end
def ordered_relation
if order_values.empty? && primary_key
if order_values.empty? && primary_key && limit_value.blank?
order(arel_attribute(primary_key).asc)
else
self

View file

@ -1265,6 +1265,21 @@ 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