Skip to content

Commit caa0b51

Browse files
authored
Merge pull request rails#52915 from rails/revert-52892-assign_ids_first
Revert "Assign id attributes first in Active Record attribute assignment"
2 parents 49b8e70 + bb0a616 commit caa0b51

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

activerecord/lib/active_record/attribute_assignment.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@ module ActiveRecord
44
module AttributeAssignment
55
private
66
def _assign_attributes(attributes)
7-
foreign_keys = foreign_key_column_names
8-
foreign_key_attributes = nil
9-
normal_attributes = {}
10-
multi_parameter_attributes = nil
7+
multi_parameter_attributes = nested_parameter_attributes = nil
118

129
attributes.each do |k, v|
1310
key = k.to_s
1411

15-
if key.in?(foreign_keys)
16-
(foreign_key_attributes ||= {})[key] = v
17-
elsif key.include?("(")
12+
if key.include?("(")
1813
(multi_parameter_attributes ||= {})[key] = v
14+
elsif v.is_a?(Hash)
15+
(nested_parameter_attributes ||= {})[key] = v
1916
else
20-
normal_attributes[key] = v
17+
_assign_attribute(key, v)
2118
end
2219
end
2320

24-
foreign_key_attributes.each { |key, value| _assign_attribute(key, value) } if foreign_key_attributes
25-
normal_attributes.each { |key, value| _assign_attribute(key, value) }
21+
assign_nested_parameter_attributes(nested_parameter_attributes) if nested_parameter_attributes
2622
assign_multiparameter_attributes(multi_parameter_attributes) if multi_parameter_attributes
2723
end
2824

29-
def foreign_key_column_names
30-
self.class._reflections.values.grep(Reflection::BelongsToReflection).map(&:foreign_key)
25+
# Assign any deferred nested attributes after the base attributes have been set.
26+
def assign_nested_parameter_attributes(pairs)
27+
pairs.each { |k, v| _assign_attribute(k, v) }
3128
end
3229

3330
# Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done

activerecord/test/cases/store_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ class StoreTest < ActiveRecord::TestCase
220220
assert_equal "blue", user.color[:jenny]
221221
end
222222

223-
test "update store and accessor" do
223+
test "store takes precedence when updating store and accessor" do
224224
user = Admin::User.find_by_name("Jamis")
225-
user.update(settings: { homepage: "not rails" }, homepage: "rails")
225+
user.update(settings: { homepage: "rails" }, homepage: "not rails")
226226

227227
assert_equal "rails", user.settings[:homepage]
228228
assert_equal "rails", user.homepage

0 commit comments

Comments
 (0)