通过 current_user 的投票或评论查找没有回复的评论
Find comments with no reply either by vote or comment by 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
问题
这是否可能以良好的性能为此创建一个 activerecord 查询?或者每次用户在新的 table 中创建评论(如 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
概览
我有一个评论列表,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
问题
这是否可能以良好的性能为此创建一个 activerecord 查询?或者每次用户在新的 table 中创建评论(如 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