提交 afcbdfc1 编写于 作者: P Peter Marklund 提交者: Michael Koziarski

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

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: NMichael Koziarski <michael@koziarski.com>
[#2543 state:committed]
上级 49a84ff6
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册