2

I have a person model which embeds_many addresses.

Is there a way to always clear embedded relationship when updating? For example, If I send a complete representation of a person, including addresses, I want to replace existing addresses rather than appending them.

My temp fix is a before_save callback which clear out all addresses

class Person
  include Mongoid::Document
  embeds_many :addresses

  before_save :clear_addresses!

  def clear_addresses!
    self.unset(:addresses)
  end
end
Tim Brunsmo
  • 581
  • 6
  • 22
  • If you do that, why having a embeds_many addresses ? because you never have this addresses associate to your person .... – shingara Apr 06 '12 at 07:33

1 Answers1

1

You can pass an empty addresses params when you update your person

person.update_attributes(:addresses => [])
shingara
  • 46,608
  • 11
  • 99
  • 105