通过投票或按current_user评论查找没有回复的评论



概述

我有一个评论列表,current_user可以在其中回复新评论或投票支持评论。现在我正在尝试创建一个视图来强制用户回复新评论或只是为评论投票。

任务

按current_user查找没有回复(投票或评论)的评论。

模型

注释模型

class Comment < ApplicationRecord
belongs_to :question
belongs_to :user
belongs_to :parent,  class_name: 'Comment', optional: true
has_many :replies, class_name: 'Comment', foreign_key: :parent_id, dependent: :destroy
has_many :votes, dependent: :destroy
end

投票模型

class Vote < ApplicationRecord
belongs_to :user
belongs_to :comment
end

问题

这是否可能具有不错的性能来为此创建活动记录查询?或者,每次用户在新表中创建注释(如pending_replies或类似内容)时创建记录,然后在创建回复时删除时,创建记录会更容易吗?

任何提示和起点都会很棒

我认为这不会太糟糕,很容易理解。

# user.rb
# Rails 4
def comments_with_no_vote_or_reply
Comment.where.not(:id => votes.pluck(:comment_id) + replies.pluck(:parent_id))
end
# Rails 3
def comments_with_no_vote_or_reply
Comment.where("comments.id not in (?)", votes.pluck(:comment_id) + replies.pluck(:parent_id))
end

最新更新