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 回答