2

If I have the following classes:

class Bar
  include Mongoid::Document
  embeds_many :foos, :cascade_callbacks => true
end

class Foo
  include Mongoid::Document
  embedded_in :bar, :inverse_of => :foos
end

Is it possible to have this in such a way that whenever a Foo is saved/updated, it's parent Bar is automatically saved (and specifically calls it's own 'before_save' callback). I want to do this without having to expose the methods of Bar to Foo.

i.e. NOT doing this as a solution:

class Bar
  include Mongoid::Document
  embeds_many :foos, :cascade_callbacks => true

  def update_stuff_on_bar
   # Stuff
  end
end

class Foo
  include Mongoid::Document
  embedded_in :bar, :inverse_of => :foos
  before_save :update_parent

  def update_parent
    bar.update_stuff_on_bar # <- We're having to use internals of Bar inside Foo. 
  end
end
RemoteCTO
  • 818
  • 8
  • 21

0 Answers0