Skip to content

Commit

Permalink
Merge pull request #6544 from flexoid/issue-6431
Browse files Browse the repository at this point in the history
exists?(false) returns false
  • Loading branch information
drogus committed Jun 25, 2012
2 parents e5abb89 + 359592b commit 9e1a199
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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:
Expand Down
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/finder_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 9e1a199

Please sign in to comment.