开发者

soft deleting mongoid document along with associated documents

I h开发者_StackOverflowave 2 models, user and posts

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  references_many :posts, :autosave => true, :dependent => :destroy
end

class Post
  include Mongoid::Document
  referenced_in :user
end

Now when I soft delete user, I also want to soft delete posts. Is there any way I can do this?

For soft deleting a document I am using Mongoid::Paranoia


Why would you want to delete the users posts? If I was following some thread (I assume that the posts are threaded), and some user, who wrote some posts in the threads, deleted his profile, I wouldn't like his posts to be removed. This would break the flow of the post-thread.

I'm aware that this doesn't answer your question, but it might be grounds for considering if you really need to delete the posts.


Would a before_destroy callback do what you need? e.g.

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  references_many :posts, :autosave => true, :dependent => :destroy
  before_destroy :delete_posts

  def delete_posts
    posts.delete_all
  end
end

class Post
  include Mongoid::Document
  include Mongoid::Paranoia
  referenced_in :user
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜