Rails: Why is it called before_save on the parent?

The task has many accounts:

class Job < ActiveRecord::Base
  has_many :invoices, :autosave => true
  before_save :set_outstanding_payments
end

class Invoice < ActiveRecord::Base
  belongs_to :job
end

When the invoice is updated ( @invoice.update(...)), the job is called set_outstanding_payments.

Why?

+3
source share
1 answer

I am really surprised at this behavior, because, as noted in the docs, autosave only starts when the parent is saved.

The reason may be that having an advertisement :autosave => truein your Job with Invoice association causes the child update to cause the parent to save .

, save hooks. , , Invoice , .

:

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

, -.

+6

All Articles