Skip to content

Commit d92ae6c

Browse files
committed
passing an ActiveRecord object to exists? is deprecated.
Pass the id of the object to the method by calling `.id` on the AR object.
1 parent e88da37 commit d92ae6c

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

activerecord/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Passing an Active Record object to `exists?` is now deprecated. Call `.id`
2+
on the object first.
3+
14
* Only use BINARY for mysql case sensitive uniqueness check when column has a case insensitive collation.
25

36
*Ryuta Kamizono*

activerecord/lib/active_record/associations/collection_association.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def include?(record)
370370
if record.new_record?
371371
include_in_memory?(record)
372372
else
373-
loaded? ? target.include?(record) : scope.exists?(record)
373+
loaded? ? target.include?(record) : scope.exists?(record.id)
374374
end
375375
else
376376
false

activerecord/lib/active_record/relation/finder_methods.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_support/deprecation'
2+
13
module ActiveRecord
24
module FinderMethods
35
ONE_AS_ONE = '1 AS one'
@@ -280,7 +282,12 @@ def forty_two!
280282
# Person.exists?(false)
281283
# Person.exists?
282284
def exists?(conditions = :none)
283-
conditions = conditions.id if Base === conditions
285+
if Base === conditions
286+
conditions = conditions.id
287+
ActiveSupport::Deprecation.warn "You are passing an instance of ActiveRecord::Base to `exists?`." \
288+
"Please pass the id of the object by calling `.id`"
289+
end
290+
284291
return false if !conditions
285292

286293
relation = apply_join_dependency(self, construct_join_dependency)

activerecord/test/cases/finder_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_exists
5656
assert_equal true, Topic.exists?(id: [1, 9999])
5757

5858
assert_equal false, Topic.exists?(45)
59-
assert_equal false, Topic.exists?(Topic.new)
59+
assert_equal false, Topic.exists?(Topic.new.id)
6060

6161
assert_raise(NoMethodError) { Topic.exists?([1,2]) }
6262
end

activerecord/test/cases/relations_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def test_exists
769769
assert ! davids.exists?(authors(:mary).id)
770770
assert ! davids.exists?("42")
771771
assert ! davids.exists?(42)
772-
assert ! davids.exists?(davids.new)
772+
assert ! davids.exists?(davids.new.id)
773773

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

0 commit comments

Comments
 (0)