Method: ActiveEntity::NestedAttributes::ClassMethods#accepts_nested_attributes_for
- Defined in:
- lib/active_entity/nested_attributes.rb
#accepts_nested_attributes_for(*attr_names) ⇒ Object
Defines an attributes writer for the specified association(s).
Supported options:
- :allow_destroy
-
If true, destroys any members from the attributes hash with a
_destroykey and a value that evaluates totrue(e.g. 1, ‘1’, true, or ‘true’). This option is off 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
trueorfalse. When no:reject_ifis specified, a record will be built for all attribute hashes that do not have a_destroyvalue that evaluates to true. Passing:all_blankinstead 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
:limitoption 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_onlyoption isfalseand the nested attributes are used to update the existing record only if they include the record’s:idvalue. Otherwise a new record will be instantiated and used to replace the existing one. However if the:update_onlyoption istrue, the nested attributes are used to update the record’s attributes always, regardless of whether the:idis 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
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/active_entity/nested_attributes.rb', line 332 def accepts_nested_attributes_for(*attr_names) = { allow_destroy: false, update_only: false } .update(attr_names.) .assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only) [:reject_if] = REJECT_ALL_BLANK_PROC if [:reject_if] == :all_blank attr_names.each do |association_name| if reflection = _reflect_on_association(association_name) = self..dup [association_name.to_sym] = self. = type = (reflection.collection? ? :collection : :one_to_one) generate_association_writer(association_name, type) else raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?" end end end |