aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Walpole <twalpole@gmail.com>2015-07-18 08:23:52 -0700
committerThomas Walpole <twalpole@gmail.com>2015-07-18 08:23:52 -0700
commitb5d4dd47deaae27e8f362bb9636246c5b4c56e5c (patch)
tree40a9aa6f8341d728d1ba918a449fd16c12a2cd3b
parent2a0a264b39eb99ddf444bbdacf3014868c8896cc (diff)
downloadrails-b5d4dd47deaae27e8f362bb9636246c5b4c56e5c.tar.gz
rails-b5d4dd47deaae27e8f362bb9636246c5b4c56e5c.tar.bz2
rails-b5d4dd47deaae27e8f362bb9636246c5b4c56e5c.zip
Ensure that 'ActionController::Parameters' can still be passed to AR for collection associations
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb6
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb14
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index c337e1d18f..a6b76b25bf 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -445,6 +445,9 @@ module ActiveRecord
# ])
def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
options = self.nested_attributes_options[association_name]
+ if attributes_collection.respond_to?(:permitted?)
+ attributes_collection = attributes_collection.to_h
+ end
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
@@ -471,6 +474,9 @@ module ActiveRecord
end
attributes_collection.each do |attributes|
+ if attributes.respond_to?(:permitted?)
+ attributes = attributes.to_h
+ end
attributes = attributes.with_indifferent_access
if attributes['id'].blank?
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index 933dfac806..b8a0401fe3 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -1074,12 +1074,16 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR
true
end
+ def [](key)
+ @hash[key]
+ end
+
def to_h
@hash
end
end
- test "strong params style objects can be assigned" do
+ test "strong params style objects can be assigned for singular associations" do
params = { name: "Stern", ship_attributes:
ProtectedParameters.new(name: "The Black Rock") }
part = ShipPart.new(params)
@@ -1087,4 +1091,12 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR
assert_equal "Stern", part.name
assert_equal "The Black Rock", part.ship.name
end
+
+ test "strong params style objects can be assigned for collection associations" do
+ params = { trinkets_attributes: ProtectedParameters.new("0" => ProtectedParameters.new(name: "Necklace"), "1" => ProtectedParameters.new(name: "Spoon")) }
+ part = ShipPart.new(params)
+
+ assert_equal "Necklace", part.trinkets[0].name
+ assert_equal "Spoon", part.trinkets[1].name
+ end
end