Skip to content

Commit

Permalink
Fix that #exists? raises ThrowResult when called with an empty limited
Browse files Browse the repository at this point in the history
reflection.

ActiveRecord::FinderMethods#construct_limited_ids_condition will raise
ThrowResult if the limited reflection comes back empty. The other callers
of #construct_limited_ids_condition handle this exception (more specifically,
the callers of construct_relation_for*), but #exists? didn't until now.
  • Loading branch information
Empact committed Jun 11, 2012
1 parent d17fa45 commit 340a93f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def exists?(id = false)
end

connection.select_value(relation, "#{name} Exists", relation.bind_values)
rescue ThrowResult
false
end

protected
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def test_exists_with_order
assert Topic.order(:id).uniq.exists?
end

def test_does_not_exist_with_empty_table_and_no_args_given
def test_exists_with_includes_limit_and_empty_result
assert !Topic.includes(:replies).limit(0).exists?
assert !Topic.includes(:replies).limit(1).where('0 = 1').exists?
end

def test_exists_with_empty_table_and_no_args_given
Topic.delete_all
assert !Topic.exists?
end
Expand Down

0 comments on commit 340a93f

Please sign in to comment.