Rails: Why before_save on parent is called?

ruby-on-rails, ruby-on-rails-4

Solution

I'm really astonished with this behavior since, as documentations said, autosaving is only triggered when the parent is saved.

The cause could be because having the `:autosave => true` declaration in your Job association with Invoice causes the child update to call save on the parent.

When the save is called on the parent, all save hooks are called. Even though, make sure you don't have any before or after update hooks in the `Invoice` model which messes with the parent.

For more information about the Autosave Association feature, please refer to this link:

http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html

I hope it helps somehow.

Problem

Job has many invoices: ``` class Job < ActiveRecord::Base has_many :invoices, :autosave => true before_save :set_outstanding_payments end class Invoice < ActiveRecord::Base belongs_to :job end ``` When invoice is updated (`@invoice.update(...)`), job's `set_outstanding_payments` is called. Why?

Original source