开发者

mongid embedded document callbacks

开发者 https://www.devze.com 2023-02-20 19:33 出处:网络
I have a following model with mongoid rails3 class Address include Mongoid::Document embedded_in :person, :inverse_of => :address

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 callba开发者_如何转开发ck 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


You can use cascade_callbacks: true on the parent document:

embeds_one :child, cascade_callbacks: true


Mongoid only fires the callback of the document that the persistence action was executed on.

Therefore, in this case, only the validation callback will fire for Address because Address is embedded in Person. The create/update callback will be called for Person.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号