Skip to content

Commit

Permalink
Merge pull request #36647 from giraffate/fix_exists_with_distinct_and…
Browse files Browse the repository at this point in the history
…_offset_and_order_in_postgresql

Fix `relation.exists?` with giving `distinct`, `offset` and `order` for joined table
  • Loading branch information
kamipo committed Jul 11, 2019
1 parent 15bfe6c commit 661cf57
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -360,7 +360,7 @@ def offset_index

def construct_relation_for_exists(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
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/finder_test.rb
Expand Up @@ -260,6 +260,11 @@ def test_exists_with_distinct_and_offset_and_select
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
Expand Down

0 comments on commit 661cf57

Please sign in to comment.