Skip to content

Instantly share code, notes, and snippets.

@demisx
Last active April 29, 2024 09:02
Show Gist options
  • Save demisx/9896113 to your computer and use it in GitHub Desktop.
Save demisx/9896113 to your computer and use it in GitHub Desktop.
When Active Record Child Objects are Autosaved in Rails

belongs_to:

  1. Assigning an object to a belongs_to association does not automatically save the object. It does not save the associated object either.

has_one:

  1. When you assign an object to a has_one association, that object is automatically saved (in order to update its foreign key).
  2. In addition, any object being replaced is also automatically saved, because its foreign key will change too
  3. If either of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.
  4. If the parent object (the one declaring the 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.
  5. If you want to assign an object to a has_one association without saving the object, use the association.build method.

has_many:

  1. When you assign an object to a 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.
  2. If either of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.
  3. If the parent object (the one declaring the 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.
  4. If you want to assign an object to a has_many association without saving the object, use the association.build method.

has_and_belongs_to_many:

  1. When you assign an object to a has_and_belongs_to_many association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.
  2. If any of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.
  3. If the parent object (the one declaring the 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 saved
  4. If you want to assign an object to a has_and_belongs_to_many association without saving the object, use the collection.build method

:autosave

If 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.

@jbodah
Copy link

jbodah commented Jul 8, 2015

Well that explains some things. Apparently has_one ... :through will autosave by default even if the :through model has a belongs_to (in Rails 4.1.5 at least)

@rsmithlal
Copy link

Thanks so much!!!!! The very last line of your doc was the answer to a validation error I was getting for an associated model while saving a model instance.

@vandamon
Copy link

vandamon commented Apr 4, 2017

autosave apparently only works while creating, not updating

@azin634
Copy link

azin634 commented Jun 17, 2017

I ran into this situation for has_one:
If either of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.

Is there documentation anywhere that explains this?

@yanielv
Copy link

yanielv commented Oct 18, 2017

@vandamon: When the autosave option isn't present, this feature only works for new records. If you specify autosave: true, also works on updates...

@moeshu
Copy link

moeshu commented May 31, 2019

Thanks man! Understanding the magic of rails always helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment