aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-07-11 09:09:03 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-07-11 09:11:01 +0900
commitba7634d304008a4e6170fd701a2b7e75e1d83aea (patch)
tree173a8512d595981e4aca83d75fc4a28490ce0af8 /activerecord
parent3fd5bf81c033eb2110533c8a713f38c0245356fb (diff)
parentd9587feb5fab65c4c5bb7a5372f7319f50565f60 (diff)
downloadrails-ba7634d304008a4e6170fd701a2b7e75e1d83aea.tar.gz
rails-ba7634d304008a4e6170fd701a2b7e75e1d83aea.tar.bz2
rails-ba7634d304008a4e6170fd701a2b7e75e1d83aea.zip
Merge pull request #36647 from giraffate/fix_exists_with_distinct_and_offset_and_order_in_postgresql
Fix `relation.exists?` with giving `distinct`, `offset` and `order` for joined table
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb5
2 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 6deb9c7da8..1dbf4808fd 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -354,7 +354,7 @@ module ActiveRecord
conditions = sanitize_forbidden_attributes(conditions)
if distinct_value && offset_value
- relation = limit(1)
+ relation = except(:order).limit!(1)
else
relation = except(:select, :distinct, :order)._select!(ONE_AS_ONE).limit!(1)
end
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 3752fd42e3..1f2058cc0a 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -283,6 +283,11 @@ class FinderTest < ActiveRecord::TestCase
assert_not Post.select(:body).distinct.offset(4).exists?
end
+ def test_exists_with_distinct_and_offset_and_eagerload_and_order
+ assert Post.eager_load(:comments).distinct.offset(10).merge(Comment.order(post_id: :asc)).exists?
+ assert_not Post.eager_load(:comments).distinct.offset(11).merge(Comment.order(post_id: :asc)).exists?
+ end
+
# Ensure +exists?+ runs without an error by excluding distinct value.
# See https://github.com/rails/rails/pull/26981.
def test_exists_with_order_and_distinct