Built-in Document Callbacks

I have the following model with mongoid rails3

class Address
  include Mongoid::Document
  embedded_in :person, :inverse_of => :address
  after_validation :call_after_validation
  before_validation :call_before_validation
  before_update :call_before_update
  after_update :call_after_update
  after_create :call_after_create
  before_create :call_before_create

  field :address1
  field :address2

  private
  def call_after_validation
    puts "After validation callback fired."
  end

  def call_before_validation
    puts "Before validation callback fired."
  end

  def call_before_update
    puts "Before update callback fired."
  end

  def call_after_update
    puts "After update callback fired."
  end

  def call_after_create
    puts "After create callback fired."
  end

  def call_before_create
    puts "Before create callback fired."
  end



end

class Person
  include Mongoid::Document
  embeds_one :address

  field :name
end

Now I used the nested form to save the face and address immediately.

But all after / before creating / updating the callbacks for the address do not start, except after / before_validation

Any suggestions as to why, after / before creating / updating, callbacks are not triggered for an address when created from a nested form?

thank

+5
source share
2 answers

Mongoid only calls the callback of the document in which the save action was performed.

, , Person. create/update Person.

+4

cascade_callbacks: true :

embeds_one: child, cascade_callbacks: true

+26

All Articles