The behavior is that associations are "autosaved" when the autosave
option is not specified, i.e. the autosave
option is by default is true
with the condition of "new association records are saved but the updated associations are not saved".
The following description taken from the Rails API docs on "Active Record Autosave Association" explains this feature clearly:
AutosaveAssociation is a module that takes care of automatically
saving associated records when their parent is saved. In addition to
saving, it also destroys any associated records that were marked for
destruction. (See mark_for_destruction and marked_for_destruction?).
Saving of the parent, its associations, and the destruction of marked
associations, all happen inside a transaction. This should never leave
the database in an inconsistent state.
If validations for any of the associations fail, their error messages
will be applied to the parent.
Note that it also means that associations marked for destruction won't
be destroyed directly. They will however still be marked for
destruction.
Note that autosave: false is not same as not declaring :autosave. When
the :autosave option is not present then new association records are
saved but the updated association records are not saved.
If you do not want the associated objects saved or destroyed when parent objects are created/updated or destroyed then specify autosave: false
when declaring associations:
class User < ActiveRecord::Base
has_one :blog, autosave: false
end
This way, when an user
is created, you won't see it's associated blog
saved. You'll have to save it manually calling save
on the associated blog
instance.