Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongid embedded document callbacks

I have a 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 nested form to save Person and Address at once.

But all after/before create/update callbacks for address are not fired except for after/before_validation

Any suggestions for why after/before create/update callbacks are not being fired for address when created from nested form?

Thanks

like image 561
Gagan Avatar asked Mar 31 '11 11:03

Gagan


1 Answers

You can use cascade_callbacks: true on the parent document:

embeds_one :child, cascade_callbacks: true

like image 142
Hollownest Avatar answered Sep 22 '22 17:09

Hollownest