Permalink
Browse files

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...
1 parent 49a84ff commit afcbdfc15f919a470e4cfca97fb0084eebd2ab1f Peter Marklund committed with NZKoz May 14, 2009
Showing with 9 additions and 8 deletions.
  1. +3 −8 activerecord/lib/active_record/base.rb
  2. +6 −0 activerecord/test/cases/finder_test.rb
@@ -697,14 +697,9 @@ def find_by_sql(sql)
# Person.exists?(['name LIKE ?', "%#{query}%"])
# Person.exists?
def exists?(id_or_conditions = {})
- connection.select_all(
- construct_finder_sql(
- :select => "#{quoted_table_name}.#{primary_key}",
- :conditions => expand_id_conditions(id_or_conditions),
- :limit => 1
- ),
- "#{name} Exists"
- ).size > 0
+ find_initial(
+ :select => "#{quoted_table_name}.#{primary_key}",
+ :conditions => expand_id_conditions(id_or_conditions)) ? true : false
end
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
@@ -119,6 +119,12 @@ def test_exists_with_aggregate_having_three_mappings_with_one_difference
Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
end
+ def test_exists_with_scoped_include
+ Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do
+ assert Developer.exists?
+ end
+ end
+
def test_find_by_array_of_one_id
assert_kind_of(Array, Topic.find([ 1 ]))
assert_equal(1, Topic.find([ 1 ]).length)

0 comments on commit afcbdfc

Please sign in to comment.