Related

Nested Attributes

Nested attributes allow you to save attributes on associated records
through the parent. By default nested attribute updating is turned off, you
can enable it using the accepts_nested_attributes_for
class method. When you enable nested attributes an attribute writer is
defined on the model.

The attribute writer is named after the association, which means that in
the following example, two new methods are added to your model:
author_attributes=(attributes) and
pages_attributes=(attributes).

By default you will only be able to set and update attributes on the
associated model. If you want to destroy the associated model through the
attributes hash, you have to enable it first using the
:allow_destroy option.

One-to-many

You can now set or update attributes on an associated post model through
the attribute hash.

For each hash that does not have an id key a new record
will be instantiated, unless the hash also contains a _destroy key
that evaluates to true.

params={:member=>{:name=>'joe',:posts_attributes=>[{:title=>'Kari, the awesome Ruby documentation browser!'},{:title=>'The egalitarian assumption of the modern citizen'},{:title=>'',:_destroy=>'1'}# this will be ignored]}}member=Member.create(params['member'])member.posts.length# => 2member.posts.first.title# => 'Kari, the awesome Ruby documentation browser!'member.posts.second.title# => 'The egalitarian assumption of the modern citizen'

You may also set a :reject_if proc to silently ignore any new record hashes
if they fail to pass your criteria. For example, the previous example could
be rewritten as:

class Member<ActiveRecord::Basehas_many:postsaccepts_nested_attributes_for:posts,:reject_if=>proc{|attributes|attributes['title'].blank?}endparams={:member=>{:name=>'joe',:posts_attributes=>[{:title=>'Kari, the awesome Ruby documentation browser!'},{:title=>'The egalitarian assumption of the modern citizen'},{:title=>''}# this will be ignored because of the :reject_if proc]}}member=Member.create(params['member'])member.posts.length# => 2member.posts.first.title# => 'Kari, the awesome Ruby documentation browser!'member.posts.second.title# => 'The egalitarian assumption of the modern citizen'Alternatively,:reject_ifalsoacceptsasymbolforusingmethods:class Member<ActiveRecord::Basehas_many:postsaccepts_nested_attributes_for:posts,:reject_if=>:new_record?endclass Member<ActiveRecord::Basehas_many:postsaccepts_nested_attributes_for:posts,:reject_if=>:reject_postsdef reject_posts(attributed)attributed['title].blank?
end
end

If the hash contains an id key that matches an already associated
record, the matching record will be modified:

By default the associated records are protected from being destroyed. If
you want to destroy any of the associated records through the attributes
hash, you have to enable it first using the :allow_destroy option.
This will allow you to also use the _destroy key to destroy
existing records:

Saving

All changes to models, including the destruction of those marked for
destruction, are saved and destroyed automatically and atomically when the
parent model is saved. This happens inside the transaction initiated by the
parents save method. See ActiveRecord::AutosaveAssociation.