Skip to content

Commit

Permalink
Changed ActiveRecord::Base#exists? to invoke find_initial so that it …
Browse files Browse the repository at this point in the history
…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
Peter Marklund authored and NZKoz committed May 14, 2009
1 parent 49a84ff commit afcbdfc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 3 additions & 8 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/finder_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit afcbdfc

Please sign in to comment.