From 4f92aa6741369f67af6340dff24e9e531c9e5542 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 16 Feb 2020 23:40:03 +0000 Subject: [PATCH] Copy argument in AttributeAssignment#attributes= Before df186bd16f0d4a798e626297277fc6b490c1419e, `assign_attributes` and `attributes=` were both defined in Active Model and both made a copy of their argument. Now `assign_attributes` is overridden in Active Record and the copy happens there instead, but `attributes=` isn't overridden. This meant that assigning nested or multi-parameters via `attributes=` would mutate the argument, which the copying was meant to prevent. --- activerecord/lib/active_record/attribute_assignment.rb | 2 ++ activerecord/test/cases/attribute_methods_test.rb | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index 2882b1b583..8468fdebcd 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -10,6 +10,8 @@ module ActiveRecord super(attributes.dup) end + alias attributes= assign_attributes + private def _assign_attributes(attributes) multi_parameter_attributes = {} diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 71b5407dcc..fdfc1be949 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -279,14 +279,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase end test "hashes are not mangled" do - new_topic = { title: "New Topic" } - new_topic_values = { title: "AnotherTopic" } + new_topic = { title: "New Topic", content: { key: "First value" } } + new_topic_values = { title: "AnotherTopic", content: { key: "Second value" } } topic = Topic.new(new_topic) assert_equal new_topic[:title], topic.title + assert_equal new_topic[:content], topic.content topic.attributes = new_topic_values assert_equal new_topic_values[:title], topic.title + assert_equal new_topic_values[:content], topic.content end test "create through factory" do