提交 59cf5e7b 编写于 作者: P Pratik Naik

Make Model.exists? use relation.exists?

上级 2c8f8355
......@@ -732,10 +732,13 @@ def find_by_sql(sql)
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
# Person.exists?
def exists?(id_or_conditions = {})
find_initial(
:select => "#{quoted_table_name}.#{primary_key}",
:conditions => expand_id_conditions(id_or_conditions)) ? true : false
def exists?(id_or_conditions = nil)
case id_or_conditions
when Array, Hash
where(id_or_conditions).exists?
else
scoped.exists?(id_or_conditions)
end
end
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
......@@ -1874,14 +1877,6 @@ def attribute_condition(quoted_column_name, argument)
end
end
# Interpret Array and Hash as conditions and anything else as an id.
def expand_id_conditions(id_or_conditions)
case id_or_conditions
when Array, Hash then id_or_conditions
else sanitize_sql(primary_key => id_or_conditions)
end
end
protected
# Scope parameters to method calls within the block. Takes a hash of method_name => parameters hash.
# method_name may be <tt>:find</tt> or <tt>:create</tt>. <tt>:find</tt> parameters may include the <tt>:conditions</tt>, <tt>:joins</tt>,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册