Skip to content

Commit f48e767

Browse files
committed
Merge pull request rails#6521 from Empact/throw-result
Fix that #exists? can blow up with ThrowResult exception
2 parents d17fa45 + 340a93f commit f48e767

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

activerecord/lib/active_record/relation/finder_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def exists?(id = false)
186186
end
187187

188188
connection.select_value(relation, "#{name} Exists", relation.bind_values)
189+
rescue ThrowResult
190+
false
189191
end
190192

191193
protected

activerecord/test/cases/finder_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def test_exists_with_order
6969
assert Topic.order(:id).uniq.exists?
7070
end
7171

72-
def test_does_not_exist_with_empty_table_and_no_args_given
72+
def test_exists_with_includes_limit_and_empty_result
73+
assert !Topic.includes(:replies).limit(0).exists?
74+
assert !Topic.includes(:replies).limit(1).where('0 = 1').exists?
75+
end
76+
77+
def test_exists_with_empty_table_and_no_args_given
7378
Topic.delete_all
7479
assert !Topic.exists?
7580
end

0 commit comments

Comments
 (0)