Skip to content

Commit d35f003

Browse files
committed
passing an instance of an AR object to find is deprecated
please pass the id of the AR object by calling `.id` on the model first.
1 parent d92ae6c commit d35f003

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
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 `find` is now deprecated. Call `.id`
2+
on the object first.
3+
14
* Passing an Active Record object to `exists?` is now deprecated. Call `.id`
25
on the object first.
36

activerecord/lib/active_record/relation/finder_methods.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ def find_with_ids(*ids)
421421
end
422422

423423
def find_one(id)
424-
id = id.id if ActiveRecord::Base === id
424+
if ActiveRecord::Base === id
425+
id = id.id
426+
ActiveSupport::Deprecation.warn "You are passing an instance of ActiveRecord::Base to `find`." \
427+
"Please pass the id of the object by calling `.id`"
428+
end
425429

426430
column = columns_hash[primary_key]
427431
substitute = connection.substitute_at(column, bind_values.length)

activerecord/test/cases/associations/eager_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,19 @@ def test_eager_load_belongs_to_quotes_table_and_column_names
407407
end
408408

409409
def test_eager_load_has_one_quotes_table_and_column_names
410-
michael = Person.all.merge!(:includes => :favourite_reference).find(people(:michael))
410+
michael = Person.all.merge!(:includes => :favourite_reference).find(people(:michael).id)
411411
references(:michael_unicyclist)
412412
assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
413413
end
414414

415415
def test_eager_load_has_many_quotes_table_and_column_names
416-
michael = Person.all.merge!(:includes => :references).find(people(:michael))
416+
michael = Person.all.merge!(:includes => :references).find(people(:michael).id)
417417
references(:michael_magician,:michael_unicyclist)
418418
assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
419419
end
420420

421421
def test_eager_load_has_many_through_quotes_table_and_column_names
422-
michael = Person.all.merge!(:includes => :jobs).find(people(:michael))
422+
michael = Person.all.merge!(:includes => :jobs).find(people(:michael).id)
423423
jobs(:magician, :unicyclist)
424424
assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
425425
end

activerecord/test/cases/relations_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def test_where_with_ar_object
637637

638638
def test_find_with_list_of_ar
639639
author = Author.first
640-
authors = Author.find([author])
640+
authors = Author.find([author.id])
641641
assert_equal author, authors.first
642642
end
643643

activerecord/test/cases/validations/uniqueness_validation_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_validate_case_insensitive_uniqueness
223223
assert t_utf8.save, "Should save t_utf8 as unique"
224224

225225
# If database hasn't UTF-8 character set, this test fails
226-
if Topic.all.merge!(:select => 'LOWER(title) AS title').find(t_utf8).title == "я тоже уникальный!"
226+
if Topic.all.merge!(:select => 'LOWER(title) AS title').find(t_utf8.id).title == "я тоже уникальный!"
227227
t2_utf8 = Topic.new("title" => "я тоже УНИКАЛЬНЫЙ!")
228228
assert !t2_utf8.valid?, "Shouldn't be valid"
229229
assert !t2_utf8.save, "Shouldn't save t2_utf8 as unique"

0 commit comments

Comments
 (0)