Permalink
Browse files

Make Model.exists? use relation.exists?

  • Loading branch information...
1 parent 2c8f835 commit 59cf5e7bf293f30ef4984842a1bce0d63870fc69 @lifo lifo committed Dec 27, 2009
Showing with 7 additions and 12 deletions.
  1. +7 −12 activerecord/lib/active_record/base.rb
@@ -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>,

0 comments on commit 59cf5e7

Please sign in to comment.