【问题标题】:acts_as_commentable_with_threading + user accountsact_as_commentable_with_threading + 用户帐户
【发布时间】:2013-09-17 14:37:21
【问题描述】:
在我的 Rails 应用程序中,我在 Step 模型中集成了acts_as_commentable_with_threading。用户有许多项目,每个项目都包含许多步骤,并且用户可以评论各个步骤。我通过在我的 step.rb 模型中添加acts_as_commentable 来实现这一点。
我最近遇到了一个用户删除其帐户的问题。是否有内置于acts_as_commentable 的东西以允许用户和他们制作的cmets 之间的关联(例如在user.rb 文件中添加类似has_many :comment_threads 的东西)。由于我没有任何关联,我开始在我的应用程序中遇到一些问题,它试图引用用户不再存在的评论。
【问题讨论】:
标签:
ruby-on-rails
acts-as-commentable
【解决方案1】:
没有内置的方法。它们被线程化的事实引发了销毁相关记录的一些决定。就我个人而言,我认为如果您有一个线程对话并且在该线程的某个位置删除了评论(因为它的用户对象之前已被删除),那么应该删除该评论的所有子线程。也就是说,我对用户执行 after_destroy 以销毁所有用户 cmets 和该用户 cmets 的所有子线程:
after_destroy 'delete_comment_threads'
def delete_comment_threads
comments = Comment.where(user_id: self.id)
comments.each do |comment|
Comment.destroy comment.children.map { |c| c.id }
end
Comment.destroy comments.map { |c| c.id }
end