mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix error message on top level update when sending an array of records
This commit is contained in:
parent
a8461b7217
commit
9912e9c594
2 changed files with 17 additions and 0 deletions
|
@ -308,6 +308,11 @@ module ActiveRecord
|
||||||
# for updating all records in a single query.
|
# for updating all records in a single query.
|
||||||
def update(id = :all, attributes)
|
def update(id = :all, attributes)
|
||||||
if id.is_a?(Array)
|
if id.is_a?(Array)
|
||||||
|
if id.any?(ActiveRecord::Base)
|
||||||
|
raise ArgumentError,
|
||||||
|
"You are passing an array of ActiveRecord::Base instances to `update`. " \
|
||||||
|
"Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`."
|
||||||
|
end
|
||||||
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
|
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
|
||||||
object.update(attributes[idx])
|
object.update(attributes[idx])
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,18 @@ class PersistenceTest < ActiveRecord::TestCase
|
||||||
assert_not_equal "1 updated", Topic.first.content
|
assert_not_equal "1 updated", Topic.first.content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_update_many_with_array_of_active_record_base_objects
|
||||||
|
error = assert_raise(ArgumentError) do
|
||||||
|
Topic.update(Topic.first(2), content: "updated")
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal "You are passing an array of ActiveRecord::Base instances to `update`. " \
|
||||||
|
"Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`.", error.message
|
||||||
|
|
||||||
|
assert_not_equal "updated", Topic.first.content
|
||||||
|
assert_not_equal "updated", Topic.second
|
||||||
|
end
|
||||||
|
|
||||||
def test_class_level_update_without_ids
|
def test_class_level_update_without_ids
|
||||||
topics = Topic.all
|
topics = Topic.all
|
||||||
assert_equal 5, topics.length
|
assert_equal 5, topics.length
|
||||||
|
|
Loading…
Reference in a new issue