aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2018-04-08 23:06:48 +0100
committerEugene Kenny <elkenny@gmail.com>2018-04-08 23:06:48 +0100
commit80a09caedc6fa67f95e78e45e7eb954f6b647b2c (patch)
tree1f5cfd6dbbb1dc0d71adec1806770ba7085bf284 /activemodel/lib/active_model
parentd729bc748811339c7e536f4ca98e91801e14c6f4 (diff)
downloadrails-80a09caedc6fa67f95e78e45e7eb954f6b647b2c.tar.gz
rails-80a09caedc6fa67f95e78e45e7eb954f6b647b2c.tar.bz2
rails-80a09caedc6fa67f95e78e45e7eb954f6b647b2c.zip
Prevent changes_to_save from mutating attributes
When an array of hashes is added to a `HashWithIndifferentAccess`, the hashes are replaced with HWIAs by mutating the array in place. If an attribute's value is an array of hashes, `changes_to_save` will convert it to an array of HWIAs as a side-effect of adding it to the changes hash. Using `merge!` instead of `[]=` fixes the problem, as `merge!` copies any array values in the provided hash instead of mutating them.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/attribute_mutation_tracker.rb2
1 files changed, 1 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/attribute_mutation_tracker.rb b/activemodel/lib/active_model/attribute_mutation_tracker.rb
index 493be5bb88..1759f004a6 100644
--- a/activemodel/lib/active_model/attribute_mutation_tracker.rb
+++ b/activemodel/lib/active_model/attribute_mutation_tracker.rb
@@ -23,7 +23,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