1

I am trying to trigger child callbacks for embeds many relations. For example:

class User
  embeds_many :phones, cascade_callbacks: true
end


class Phone
  embedded_in :user, inverse_of: :phones
  before_save :callback_after_save

  def callback_after_save
    #Do some stuff here
    puts "callback fired"
  end
end

When I do

User.last.save

I see

=>true

The callbacks for the phones are not fired as they have not been changed.(performance issues sighted by mongoid)

Is there any way of forcing callbacks to be fired for each phone when the user is saved (ignoring performance issues)?

nightf0x
  • 1,969
  • 3
  • 17
  • 24

1 Answers1

0

I wrote an after save callback that would do an update_attributes on the child objects, which then triggers their callbacks.

There are performance ramifications for this, so profile it.

cpuguy83
  • 5,794
  • 4
  • 18
  • 24
  • If i understand this correctly, I would have a before_save on User which do self.phones.each {|p| p.update_attributes}. But I wouldn't want to keep doing this every time I add a new relation to User. Is there any generic way of doing this? – nightf0x Jul 03 '13 at 11:50