assign_nested_attributes_for_collection_association(association_name, attributes_collection) private

Assigns the given attributes to the collection association.

Hashes with an :id value matching an existing associated record will update that record. Hashes without an :id value will build a new record for the association. Hashes with a matching :id value and a :_destroy key set to a truthy value will mark the matched record for destruction.

For example:

assign_nested_attributes_for_collection_association(:people, {
  '1' => { id: '1', name: 'Peter' },
  '2' => { name: 'John' },
  '3' => { id: '2', _destroy: true }
})

Will update the name of the Person with ID 1, build a new associated person with the name ‘John’, and mark the associated Person with ID 2 for destruction.

Also accepts an Array of attribute hashes:

assign_nested_attributes_for_collection_association(:people, [
  { id: '1', name: 'Peter' },
  { name: 'John' },
  { id: '2', _destroy: true }
])
Show source
Register or log in to add new notes.