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
4

1 に答える 1

1

addresses個人を更新するときに空のパラメーターを渡すことができます

person.update_attributes(:addresses => [])
于 2012-04-06T07:33:18.587 に答える