Restore an ability that class level update
without giving ids
That ability was introduced at #11898 as `Relation#update` without giving ids, so the ability on the class level is not documented and not tested. c83e30d which fixes #33470 has lost two undocumented abilities. One has fixed at 5c65688, but I missed the ability on the class level. Removing any feature should not be suddenly happened in a stable version even if that is not documented. I've restored the ability and added test case to avoid any regression in the future. Fixes #34743.
This commit is contained in:
parent
600a66f60c
commit
4c05434973
@ -96,11 +96,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,
|
||||
|
@ -53,6 +53,20 @@ def test_update_many_with_invalid_id
|
||||
assert_not_equal "2 updated", Topic.find(2).content
|
||||
end
|
||||
|
||||
def test_class_level_update_without_ids
|
||||
topics = Topic.all
|
||||
assert_equal 5, topics.length
|
||||
topics.each do |topic|
|
||||
assert_not_equal "updated", topic.content
|
||||
end
|
||||
|
||||
updated = Topic.update(content: "updated")
|
||||
assert_equal 5, updated.length
|
||||
updated.each do |topic|
|
||||
assert_equal "updated", topic.content
|
||||
end
|
||||
end
|
||||
|
||||
def test_class_level_update_is_affected_by_scoping
|
||||
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user