aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-10-15 08:09:18 -0700
committerSean Griffin <sean@seantheprogrammer.com>2015-10-15 08:09:18 -0700
commitde92b06bca9e744525452dd83e13a784bcfb9d3e (patch)
tree4348dfc3a81f4843219259e6326b510ad0bd8f71
parentb6cf69ebcbf7372c61d38aa33baa7d0f4224679b (diff)
parentb901a49473817339e892f6c237a365a361b0b2d7 (diff)
downloadrails-de92b06bca9e744525452dd83e13a784bcfb9d3e.tar.gz
rails-de92b06bca9e744525452dd83e13a784bcfb9d3e.tar.bz2
rails-de92b06bca9e744525452dd83e13a784bcfb9d3e.zip
Merge pull request #21964 from Drenmi/enhancement/update-deprecation-message
Better deprecation warning for `ActiveRecord::Relation#update`
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/relation.rb7
-rw-r--r--activerecord/test/cases/relations_test.rb7
3 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 026bb71949..afbbc3d2ec 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* 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`.
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 05bf87ab9d..392b462aa9 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -417,6 +417,13 @@ module ActiveRecord
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
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 8256762f96..7521f0573a 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1541,6 +1541,13 @@ class RelationTest < ActiveRecord::TestCase
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')