Permalink
Browse files

passing an ActiveRecord object to `exists?` is deprecated.

Pass the id of the object to the method by calling `.id` on the AR
object.
  • Loading branch information...
1 parent e88da37 commit d92ae6ccca3bcfd73546d612efaea011270bd270 @tenderlove tenderlove committed Mar 13, 2014
@@ -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*
@@ -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
@@ -1,3 +1,5 @@
+require 'active_support/deprecation'
+
module ActiveRecord
module FinderMethods
ONE_AS_ONE = '1 AS one'
@@ -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)
@@ -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
@@ -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?

0 comments on commit d92ae6c

Please sign in to comment.