mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Raise ArgumentError when a instance of ActiveRecord::Base is passed to
find and exists?
This commit is contained in:
parent
bc87cd7f18
commit
4b6709e818
4 changed files with 18 additions and 19 deletions
|
@ -1,3 +1,8 @@
|
|||
* Raise `ArgumentError` when passing an `ActiveRecord::Base` instance to `.find`
|
||||
and `.exists?`.
|
||||
|
||||
*Rafael Mendonça França*
|
||||
|
||||
* Respect precision option for arrays of timestamps.
|
||||
|
||||
Fixes #27514.
|
||||
|
|
|
@ -171,23 +171,19 @@ module ActiveRecord
|
|||
return super if block_given? ||
|
||||
primary_key.nil? ||
|
||||
scope_attributes? ||
|
||||
columns_hash.include?(inheritance_column) ||
|
||||
ids.first.kind_of?(Array)
|
||||
columns_hash.include?(inheritance_column)
|
||||
|
||||
id = ids.first
|
||||
if ActiveRecord::Base === id
|
||||
id = id.id
|
||||
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
||||
You are passing an instance of ActiveRecord::Base to `find`.
|
||||
Please pass the id of the object by calling `.id`.
|
||||
MSG
|
||||
end
|
||||
|
||||
return super if id.kind_of?(Array) ||
|
||||
id.is_a?(ActiveRecord::Base)
|
||||
|
||||
key = primary_key
|
||||
|
||||
statement = cached_find_by_statement(key) { |params|
|
||||
where(key => params.bind).limit(1)
|
||||
}
|
||||
|
||||
record = statement.execute([id], self, connection).first
|
||||
unless record
|
||||
raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
|
||||
|
|
|
@ -301,8 +301,7 @@ module ActiveRecord
|
|||
# Person.exists?
|
||||
def exists?(conditions = :none)
|
||||
if Base === conditions
|
||||
conditions = conditions.id
|
||||
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
||||
raise ArgumentError, <<-MSG.squish
|
||||
You are passing an instance of ActiveRecord::Base to `exists?`.
|
||||
Please pass the id of the object by calling `.id`.
|
||||
MSG
|
||||
|
@ -456,11 +455,10 @@ module ActiveRecord
|
|||
|
||||
def find_one(id)
|
||||
if ActiveRecord::Base === id
|
||||
id = id.id
|
||||
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
||||
You are passing an instance of ActiveRecord::Base to `find`.
|
||||
Please pass the id of the object by calling `.id`.
|
||||
MSG
|
||||
raise ArgumentError, <<-MSG.squish
|
||||
You are passing an instance of ActiveRecord::Base to `find`.
|
||||
Please pass the id of the object by calling `.id`.
|
||||
MSG
|
||||
end
|
||||
|
||||
relation = where(primary_key => id)
|
||||
|
|
|
@ -118,7 +118,7 @@ class FinderTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_find_passing_active_record_object_is_deprecated
|
||||
assert_deprecated do
|
||||
assert_raises(ArgumentError) do
|
||||
Topic.find(Topic.last)
|
||||
end
|
||||
end
|
||||
|
@ -167,8 +167,8 @@ class FinderTest < ActiveRecord::TestCase
|
|||
assert_equal false, relation.exists?(false)
|
||||
end
|
||||
|
||||
def test_exists_passing_active_record_object_is_deprecated
|
||||
assert_deprecated do
|
||||
def test_exists_passing_active_record_object_is_not_permited
|
||||
assert_raises(ArgumentError) do
|
||||
Topic.exists?(Topic.new)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue