When Active Record Child Objects are Autosaved in Rails
belongs_to
association does not automatically save the object. It does not save the associated object either.has_one
association, that object is automatically saved (in order to update its foreign key).has_one
association) is unsaved (that is, new_record?
returns true) then the child objects are not saved. They will automatically when the parent object is saved.association.build
method.has_many
association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.has_many
association) is unsaved (that is, new_record?
returns true) then the child objects are not saved. They will automatically when the parent object is saved.has_many
association without saving the object, use the association.build
method.has_and_belongs_to_many
association) is unsaved (that is,new_record
?
returns true) then the child objects are not saved when they are added. All unsaved members of the association
will automatically be saved when the parent is savedhas_and_belongs_to_many
association without saving the object,
use the collection.build
methodIf true, always save the associated object or destroy it if marked for destruction, when saving the parent object. If false, never save or destroy the associated object. By default, only save the associated object if it's a new record.
Note that accepts_nested_attributes_for
sets :autosave
to true.