提交 5c656889 编写于 作者: R Ryuta Kamizono

Just delegate `update` with ids on a relation to `klass.update`

This restores an ability that `update` with ids on a relation which is
described at https://github.com/rails/rails/issues/33470#issuecomment-411203013.

I personally think that the `update` with two arguments on a relation is
not a designed feature, since that is totally not using a relation
state, and also is not documented.

But removing any feature should not be suddenly happened in a stable
version even if that is not documented.
上级 8f2caec4
......@@ -375,8 +375,12 @@ def update_all(updates)
@klass.connection.update stmt, "#{@klass} Update All"
end
def update(attributes) # :nodoc:
each { |record| record.update(attributes) }
def update(id = :all, attributes) # :nodoc:
if id == :all
each { |record| record.update(attributes) }
else
klass.update(id, attributes)
end
end
def update_counters(counters) # :nodoc:
......
......@@ -1587,6 +1587,24 @@ def test_update_on_relation
assert_equal "David", topic2.reload.author_name
end
def test_update_with_ids_on_relation
topic1 = TopicWithCallbacks.create!(title: "arel", author_name: nil)
topic2 = TopicWithCallbacks.create!(title: "activerecord", author_name: nil)
topics = TopicWithCallbacks.none
topics.update(
[topic1.id, topic2.id],
[{ title: "adequaterecord" }, { 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
assert_equal "David", topic1.reload.author_name
assert_equal "David", topic2.reload.author_name
end
def test_update_on_relation_passing_active_record_object_is_not_permitted
topic = Topic.create!(title: "Foo", author_name: nil)
assert_raises(ArgumentError) do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册