Skip to content
This repository
Browse code

Fix that #exists? raises ThrowResult when called with an empty limited

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...
commit 340a93f5026ffb8e877024c23791e8a345605976 1 parent d17fa45
Ben Woosley authored May 28, 2012
2  activerecord/lib/active_record/relation/finder_methods.rb
@@ -186,6 +186,8 @@ def exists?(id = false)
186 186
       end
187 187
 
188 188
       connection.select_value(relation, "#{name} Exists", relation.bind_values)
  189
+    rescue ThrowResult
  190
+      false
189 191
     end
190 192
 
191 193
     protected
7  activerecord/test/cases/finder_test.rb
@@ -69,7 +69,12 @@ def test_exists_with_order
69 69
     assert Topic.order(:id).uniq.exists?
70 70
   end
71 71
 
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
73 78
     Topic.delete_all
74 79
     assert !Topic.exists?
75 80
   end

0 notes on commit 340a93f

Please sign in to comment.
Something went wrong with that request. Please try again.