Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 0380e9ca5fa872e56d0920e6255a2f20b6e01030 1 parent 35e1785
Peter Marklund authored NZKoz committed
View
11 activerecord/lib/active_record/base.rb
@@ -687,14 +687,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.
View
6 activerecord/test/cases/finder_test.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.