开发者

"Cascade deleting" in mongoid

开发者 https://www.devze.com 2023-02-22 19:12 出处:网络
For example User references_many :answers Answer references_many :users Say we are deleting answers for a given user, how do we do it such that both the references on the user开发者_高级运维/answe

For example

User
  references_many :answers

Answer
  references_many :users

Say we are deleting answers for a given user, how do we do it such that both the references on the user开发者_高级运维/answer objects are deleted?


Cascading Removals

Similar to ActiveRecord, if you want child relational associations to be deleted when the parent record is deleted, simply supply the :dependent option on the references_one or references_many macro.

class User
  include Mongoid::Document
  references_one :profile, :dependent => :destroy
  references_many :answers, :dependent => :delete
end
0

精彩评论

暂无评论...
验证码 换一张
取 消