Skip to content

Commit

Permalink
Make Model.exists? use relation.exists?
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Dec 27, 2009
1 parent 2c8f835 commit 59cf5e7
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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>,
Expand Down

0 comments on commit 59cf5e7

Please sign in to comment.