我有以下型号的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
现在,我使用嵌套形式一次保存“人”和“地址”。
但是除了after/before_validation以外,不会触发所有针对address的after/before create/update回调。
关于从嵌套表单创建时为什么不触发创建/更新回调之后/之前的地址的任何建议?
谢谢
最佳答案
Mongoid仅触发执行持久性操作的文档的回调。
因此,在这种情况下,因为Address嵌入在Person中,所以仅验证回调将针对Address触发。创建/更新回调将被Person调用。
关于ruby-on-rails - mongoid嵌入式文档回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5499013/