accepts_nested_attributes_for
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-6)
- 3.1.0 (0)
- 3.2.1 (3)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (38)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (2)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
accepts_nested_attributes_for(*attr_names)
public
Defines an attributes writer for the specified association(s).
Supported options:
- :allow_destroy
-
If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (e.g. 1, ‘1’, true, or ‘true’). This option is false by default.
- :reject_if
-
Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash. The hash is passed to the supplied Proc or the method and it should return either true or false. When no :reject_if is specified, a record will be built for all attribute hashes that do not have a _destroy value that evaluates to true. Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank excluding any value for _destroy.
- :limit
-
Allows you to specify the maximum number of associated records that can be processed with the nested attributes. Limit also can be specified as a Proc or a Symbol pointing to a method that should return a number. If the size of the nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords exception is raised. If omitted, any number of associations can be processed. Note that the :limit option is only applicable to one-to-many associations.
- :update_only
-
For a one-to-one association, this option allows you to specify how nested attributes are going to be used when an associated record already exists. In general, an existing record may either be updated with the new set of attribute values or be replaced by a wholly new record containing those values. By default the :update_only option is false and the nested attributes are used to update the existing record only if they include the record’s :id value. Otherwise a new record will be instantiated and used to replace the existing one. However if the :update_only option is true, the nested attributes are used to update the record’s attributes always, regardless of whether the :id is present. The option is ignored for collection associations.
Examples:
# creates avatar_attributes= accepts_nested_attributes_for :avatar, reject_if: proc { |attributes| attributes['name'].blank? } # creates avatar_attributes= accepts_nested_attributes_for :avatar, reject_if: :all_blank # creates avatar_attributes= and posts_attributes= accepts_nested_attributes_for :avatar, :posts, allow_destroy: true
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:
http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
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
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
instead.
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
end
Reject If
reject_if: proc { |attributes| attributes[‘name’].blank? } Has saved me after an 3 hours of google search. Thanks
Rails documentation for nested attributes
ActiveRecord/NestedAttributes/ClassMethods
(don’t follow this link, the url interpreter isn’t rendering it correctly :(, but the correct link is at the top of this page)
More Docs and Explanation
You probably want to look at the class level docs
http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods
(cut and paste, apidocks can’t render the above for some reason)
Nested Model Forms
For a good example of nested model forms check out the rails blog.