Skip to content
This repository
Browse code

Changed ActiveRecord::Base#exists? to invoke find_initial so that it …

…is compatible with, and doesn't lose, :include scopes (references to eager loaded tables)

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2543 state:committed]
  • Loading branch information...
commit afcbdfc15f919a470e4cfca97fb0084eebd2ab1f 1 parent 49a84ff
authored May 14, 2009 NZKoz committed May 14, 2009
11  activerecord/lib/active_record/base.rb
@@ -697,14 +697,9 @@ def find_by_sql(sql)
697 697
       #   Person.exists?(['name LIKE ?', "%#{query}%"])
698 698
       #   Person.exists?
699 699
       def exists?(id_or_conditions = {})
700  
-        connection.select_all(
701  
-          construct_finder_sql(
702  
-            :select     => "#{quoted_table_name}.#{primary_key}",
703  
-            :conditions => expand_id_conditions(id_or_conditions),
704  
-            :limit      => 1
705  
-          ),
706  
-          "#{name} Exists"
707  
-        ).size > 0
  700
+        find_initial(
  701
+          :select => "#{quoted_table_name}.#{primary_key}",
  702
+          :conditions => expand_id_conditions(id_or_conditions)) ? true : false
708 703
       end
709 704
 
710 705
       # Creates an object (or multiple objects) and saves it to the database, if validations pass.
6  activerecord/test/cases/finder_test.rb
@@ -119,6 +119,12 @@ def test_exists_with_aggregate_having_three_mappings_with_one_difference
119 119
       Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
120 120
   end
121 121
 
  122
+  def test_exists_with_scoped_include
  123
+    Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do
  124
+      assert Developer.exists?
  125
+    end
  126
+  end
  127
+
122 128
   def test_find_by_array_of_one_id
123 129
     assert_kind_of(Array, Topic.find([ 1 ]))
124 130
     assert_equal(1, Topic.find([ 1 ]).length)

0 notes on commit afcbdfc

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