Reputation: 171409
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?
Upvotes: 3
Views: 243
Reputation: 1483
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.
Upvotes: 6