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

Ignore empty condition on #construct_relation_for_exists

At fc0e3354af,

```rb
relation = relation.where(conditions)
```

was rewritten to:

```rb
relation.where!(condition)
```

This change accidentally changed the result of `Topic.exists?({})` from true to false.

To fix this regression, first I moved the blank check logic (`opts.blank?`) from `#where` to `#where!`,
because I thought `#where!` should be identical to `#where`, except that instead of returning a new relation,
it adds the condition to the existing relation.

But on second thought after some discussion on https://github.com/rails/rails/pull/34329,
I started to think that just fixing `#construct_relation_for_exists` is more preferable
than changing `#where` and `#where!`.
This commit is contained in:
r7kamura 2018-10-27 14:35:51 +09:00
parent 5431e17733
commit 4694fcf413
2 changed files with 5 additions and 1 deletions

View file

@ -363,7 +363,7 @@ module ActiveRecord
case conditions
when Array, Hash
relation.where!(conditions)
relation.where!(conditions) unless conditions.empty?
else
relation.where!(primary_key => conditions) unless conditions == :none
end

View file

@ -246,6 +246,10 @@ class FinderTest < ActiveRecord::TestCase
assert_equal true, Topic.first.replies.exists?
end
def test_exists_with_empty_hash_arg
assert_equal true, Topic.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