Revert "Avoid extra scoping when using `Relation#update`"

This reverts commit 0b29a421.

Reason: This introduce a breaking change when you call `update` in a
relation or an association proxy passing the id argument.

Person.books.update(10, title: 'New title')
上级 3562b105
......@@ -97,11 +97,13 @@ def instantiate(attributes, column_types = {}, &block)
# When running callbacks is not needed for each record update,
# it is preferred to use {update_all}[rdoc-ref:Relation#update_all]
# for updating all records in a single query.
def update(id, attributes)
def update(id = :all, attributes)
if id.is_a?(Array)
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
object.update(attributes[idx])
}
elsif id == :all
all.each { |record| record.update(attributes) }
else
if ActiveRecord::Base === id
raise ArgumentError,
......
......@@ -337,10 +337,6 @@ def update_all(updates)
@klass.connection.update stmt, "#{@klass} Update All"
end
def update(attributes) # :nodoc:
each { |record| record.update(attributes) }
end
# Destroys the records by instantiating each
# record and calling its {#destroy}[rdoc-ref:Persistence#destroy] method.
# Each object's callbacks are executed (including <tt>:dependent</tt> association options).
......
......@@ -28,9 +28,7 @@ class RelationTest < ActiveRecord::TestCase
class TopicWithCallbacks < ActiveRecord::Base
self.table_name = :topics
cattr_accessor :topic_count
before_update { |topic| topic.author_name = "David" if topic.author_name.blank? }
after_update { |topic| topic.class.topic_count = topic.class.count }
end
def test_do_not_double_quote_string_id
......@@ -1504,8 +1502,6 @@ def test_update_on_relation
topics = TopicWithCallbacks.where(id: [topic1.id, topic2.id])
topics.update(title: "adequaterecord")
assert_equal TopicWithCallbacks.count, TopicWithCallbacks.topic_count
assert_equal "adequaterecord", topic1.reload.title
assert_equal "adequaterecord", topic2.reload.title
# Testing that the before_update callbacks have run
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册