From 83f755ff66b9152eb0c66b279757310040b23d18 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Mon, 3 Oct 2016 10:08:04 -0700 Subject: tests for use of primary_key with limit --- activerecord/test/cases/finder_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 39ffe12ec6..2f256dd36a 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -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 -- cgit v1.2.3 From 022eadddbac826cf663630d58b689ccdcb5e3777 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 9 Mar 2016 19:40:23 -0800 Subject: don't impose primary key order if limit() is defined --- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index c5562c1ff0..93f3b67686 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 + if order_values.empty? && primary_key && limit_value.blank? order(arel_attribute(primary_key).asc) else self -- cgit v1.2.3 From 06d506f0d001ef09120212e4a15cd3e6437ff876 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 9 Mar 2016 19:41:44 -0800 Subject: CHANGELOG entry --- activerecord/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d1ae41ab97..456f569718 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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 -- cgit v1.2.3