Skip to content

Commit

Permalink
passing an ActiveRecord object to exists? is deprecated.
Browse files Browse the repository at this point in the history
Pass the id of the object to the method by calling `.id` on the AR
object.
  • Loading branch information
tenderlove committed Mar 13, 2014
1 parent e88da37 commit d92ae6c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Passing an Active Record object to `exists?` is now deprecated. Call `.id`
on the object first.

* Only use BINARY for mysql case sensitive uniqueness check when column has a case insensitive collation.

*Ryuta Kamizono*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def include?(record)
if record.new_record?
include_in_memory?(record)
else
loaded? ? target.include?(record) : scope.exists?(record)
loaded? ? target.include?(record) : scope.exists?(record.id)
end
else
false
Expand Down
9 changes: 8 additions & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_support/deprecation'

module ActiveRecord
module FinderMethods
ONE_AS_ONE = '1 AS one'
Expand Down Expand Up @@ -280,7 +282,12 @@ def forty_two!
# Person.exists?(false)
# Person.exists?
def exists?(conditions = :none)
conditions = conditions.id if Base === conditions
if Base === conditions
conditions = conditions.id
ActiveSupport::Deprecation.warn "You are passing an instance of ActiveRecord::Base to `exists?`." \
"Please pass the id of the object by calling `.id`"
end

return false if !conditions

relation = apply_join_dependency(self, construct_join_dependency)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_exists
assert_equal true, Topic.exists?(id: [1, 9999])

assert_equal false, Topic.exists?(45)
assert_equal false, Topic.exists?(Topic.new)
assert_equal false, Topic.exists?(Topic.new.id)

assert_raise(NoMethodError) { Topic.exists?([1,2]) }
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def test_exists
assert ! davids.exists?(authors(:mary).id)
assert ! davids.exists?("42")
assert ! davids.exists?(42)
assert ! davids.exists?(davids.new)
assert ! davids.exists?(davids.new.id)

fake = Author.where(:name => 'fake author')
assert ! fake.exists?
Expand Down

0 comments on commit d92ae6c

Please sign in to comment.