【问题标题】:Find comments with no reply either by vote or comment by current_user通过 current_user 投票或评论查找没有回复的评论
【发布时间】:2017-04-10 18:03:26
【问题描述】:

概述

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

任务

查找当前用户没有回复(投票或评论)的 cmets。

型号

评论模型

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 查询?或者,每次用户在 pending_replies 之类的新表中创建评论时创建记录会更容易,然后在创建回复时删除?

任何提示和起点都会令人惊叹

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4


    【解决方案1】:

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

    # 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
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2013-05-13
      • 2022-06-10
      相关资源
      最近更新 更多