Permalink
Browse files

exists?(false) returns false

`FinderMethods#exists?` finder method now returns *false* with the *false* argument
  • Loading branch information...
1 parent 24a4cc7 commit 359592bba6cb85b43ca8945917b1ca57fa7494a9 @flexoid flexoid committed May 29, 2012
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* `FinderMethods#exists?` now returns `false` with the `false` argument.
+
+ *Egor Lynko*
+
* Added support for specifying the precision of a timestamp in the postgresql
adapter. So, instead of having to incorrectly specify the precision using the
`:limit` option, you may use `:precision`, as intended. For example, in a migration:
@@ -170,19 +170,19 @@ def all
# Person.exists?(['name LIKE ?', "%#{query}%"])
# Person.exists?(:name => "David")
# Person.exists?
- def exists?(id = false)
- id = id.id if ActiveRecord::Model === id
- return false if id.nil?
+ def exists?(conditions = :none)
+ conditions = conditions.id if ActiveRecord::Model === conditions
+ return false if !conditions
join_dependency = construct_join_dependency_for_association_find
relation = construct_relation_for_association_find(join_dependency)
relation = relation.except(:select, :order).select("1 AS one").limit(1)
- case id
+ case conditions
when Array, Hash
- relation = relation.where(id)
+ relation = relation.where(conditions)
else
- relation = relation.where(table[primary_key].eq(id)) if id
+ relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none
end
connection.select_value(relation, "#{name} Exists", relation.bind_values)
@@ -55,6 +55,10 @@ def test_exists_returns_true_with_one_record_and_no_args
assert Topic.exists?
end
+ def test_exists_returns_false_with_false_arg
+ assert !Topic.exists?(false)
+ end
+
# exists? should handle nil for id's that come from URLs and always return false
# (example: Topic.exists?(params[:id])) where params[:id] is nil
def test_exists_with_nil_arg

0 comments on commit 359592b

Please sign in to comment.