Skip to content

Commit 27da612

Browse files
author
Mark
committed
Modified comments explaining what the update_only option does.
1 parent acf7e86 commit 27da612

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

activerecord/lib/active_record/nested_attributes.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,18 @@ module ClassMethods
248248
# exception is raised. If omitted, any number associations can be processed.
249249
# Note that the :limit option is only applicable to one-to-many associations.
250250
# [:update_only]
251-
# Allows you to specify that an existing record may only be updated.
252-
# A new record may only be created when there is no existing record.
253-
# This option only works for one-to-one associations and is ignored for
254-
# collection associations. This option is off by default.
251+
# For a one-to-one association, this option allows you to specify how
252+
# nested attributes are to be used when an associated record already
253+
# exists. In general, an existing record may either be updated with the
254+
# new set of attribute values or be replaced by a wholly new record
255+
# containing those values. By default the :update_only option is +false+
256+
# and the nested attributes are used to update the existing record only
257+
# if they include the record's <tt>:id</tt> value. Otherwise a new
258+
# record will be instantiated and used to replace the existing one.
259+
# However if the :update_only option is +true+, the nested attributes
260+
# are used to update the record's attributes always, regardless of
261+
# whether the <tt>:id</tt> is present. The option is ignored for collection
262+
# associations.
255263
#
256264
# Examples:
257265
# # creates avatar_attributes=
@@ -312,10 +320,13 @@ def _destroy
312320

313321
# Assigns the given attributes to the association.
314322
#
315-
# If update_only is false and the given attributes include an <tt>:id</tt>
316-
# that matches the existing record's id, then the existing record will be
317-
# modified. If update_only is true, a new record is only created when no
318-
# object exists. Otherwise a new record will be built.
323+
# If an associated record does not yet exist, one will be instantiated. If
324+
# an associated record already exists, the method's behavior depends on
325+
# the value of the update_only option. If update_only is +false+ and the
326+
# given attributes include an <tt>:id</tt> that matches the existing record's
327+
# id, then the existing record will be modified. If no <tt>:id</tt> is provided
328+
# it will be replaced with a new record. If update_only is +true+ the existing
329+
# record will be modified regardless of whether an <tt>:id</tt> is provided.
319330
#
320331
# If the given attributes include a matching <tt>:id</tt> attribute, or
321332
# update_only is true, and a <tt>:_destroy</tt> key set to a truthy value,

0 commit comments

Comments
 (0)