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
You can use cascade_callbacks: true on the parent document:
embeds_one :child, cascade_callbacks: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With