mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #32498 from eugeneius/mutation_tracker_merge_changes
Prevent changes_to_save from mutating attributes
This commit is contained in:
commit
eb3740dcb0
2 changed files with 9 additions and 1 deletions
|
@ -27,7 +27,7 @@ module ActiveModel
|
|||
attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
|
||||
change = change_to_attribute(attr_name)
|
||||
if change
|
||||
result[attr_name] = change
|
||||
result.merge!(attr_name => change)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -473,6 +473,14 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_changes_to_save_should_not_mutate_array_of_hashes
|
||||
topic = Topic.new(author_name: "Bill", content: [{ a: "a" }])
|
||||
|
||||
topic.changes_to_save
|
||||
|
||||
assert_equal [{ a: "a" }], topic.content
|
||||
end
|
||||
|
||||
def test_previous_changes
|
||||
# original values should be in previous_changes
|
||||
pirate = Pirate.new
|
||||
|
|
Loading…
Reference in a new issue