accepts_nested_attributes_for(*attr_names) public

Defines an attributes writer for the specified association(s). If you are using attr_protected or attr_accessible, then you will need to add the attribute writer to the allowed list.

Supported options:

If true, destroys any members from the attributes hash with a _delete key and a value that evaluates to true (eg. 1, ‘1’, true, or ‘true’). This option is off by default.
Allows you to specify a Proc that checks whether a record should be built for a certain attribute hash. The hash is passed to the Proc and the Proc should return either true or false. When no Proc is specified a record will be built for all attribute hashes that do not have a _delete that evaluates to true.


  # creates avatar_attributes=
  accepts_nested_attributes_for :avatar, :reject_if => proc { |attributes| attributes['name'].blank? }
  # creates avatar_attributes= and posts_attributes=
  accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true
Show source
Register or log in to add new notes.
April 28, 2009 - (>= v2.3.2)
4 thanks

A very thorough explanation of use

Ryan Daigle has a great article about 2.3’s new nest forms which does a really good job of explaining how to use this and some of the potential gotchas. Highly recommended:


June 30, 2009
3 thanks

Be careful with name of attribute writer

If restricting access to attributes you normally get code like

attr_accessible :foo,  

When using these nested attributes you end up with code like

attr_accessible :foo, :bar_attributes

Its very easy to leave of the _attributes suffix e.g

attr_accessible :foo, :bar

which will cause you all sorts of problems

March 8, 2011 - (v2.3.8 - v3.0.5)
2 thanks

Dont use _delete

Most blog articles about accepts_nested_attributes_for, including the one from @mattsa and @annaswims, tell you to add a

'_delete' => 1

when you want a deletion checkbox, hidden attribute, etc.

But this stopped being true a while ago. This is just a “Watch Out!” Make sure you use

'_destroy' => 1


November 12, 2010
1 thank

nested attribute gotcha

Just adding to what @diabolist said :

class House

has_many :doors
accepts_nested_attributes_for :doors

attr_accesible :address, :doors_attributes
# NOTE its plural


November 18, 2009
0 thanks

More Docs and Explanation

You probably want to look at the class level docs


(cut and paste, apidocks can’t render the above for some reason)

November 12, 2009
0 thanks

Rails documentation for nested attributes


(don’t follow this link, the url interpreter isn’t rendering it correctly :(, but the correct link is at the top of this page)

March 20, 2009 - (>= v2.3.2)
0 thanks

Nested Model Forms

For a good example of nested model forms check out the rails blog.