提交 de92b06b 编写于 作者: S Sean Griffin

Merge pull request #21964 from Drenmi/enhancement/update-deprecation-message

Better deprecation warning for `ActiveRecord::Relation#update`
* Give `AcriveRecord::Relation#update` its own deprecation warning when
passed an `ActiveRecord::Base` instance.
Fixes #21945.
*Ted Johansson*
* Make it possible to pass `:to_table` when adding a foreign key through
`add_reference`.
......
......@@ -417,6 +417,13 @@ def update(id = :all, attributes)
elsif id == :all
to_a.each { |record| record.update(attributes) }
else
if ActiveRecord::Base === id
id = id.id
ActiveSupport::Deprecation.warn(<<-MSG.squish)
You are passing an instance of ActiveRecord::Base to `update`.
Please pass the id of the object by calling `.id`
MSG
end
object = find(id)
object.update(attributes)
object
......
......@@ -1541,6 +1541,13 @@ def test_update_on_relation
assert_equal 'David', topic2.reload.author_name
end
def test_update_on_relation_passing_active_record_object_is_deprecated
topic = Topic.create!(title: 'Foo', author_name: nil)
assert_deprecated(/update/) do
Topic.where(id: topic.id).update(topic, title: 'Bar')
end
end
def test_distinct
tag1 = Tag.create(:name => 'Foo')
tag2 = Tag.create(:name => 'Foo')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册