查找其他用户在post上收到的用户总票数

我正在使用这个gem评论: https : //github.com/lml/commontator

哪个设置很容易插入这个gem投票评论: https : //github.com/ryanto/acts_as_votable

我正在使用rails 4 btw,它与两个gem兼容。

在我的用户模型中:

class User < ActiveRecord::Base acts_as_voter acts_as_commentator has_many :comments end 

在我的评论模型中:

 class Comment < ActiveRecord::Base acts_as_votable belongs_to :user end 

一切似乎都很好。 但是在尝试计算用户总票数时(用户在所有评论中收到的总票数)(业力)

  

我收到这个错误

 undefined method `votes' for # 

所以我尝试了这个:

  

这导致了另一个错误:

 SQLite3::SQLException: no such column: commontator_comments.commontator_id: SELECT "commontator_comments".* FROM "commontator_comments" WHERE "commontator_comments"."commontator_id" = ? AND "commontator_comments"."commontator_type" = ? 

我试过了:

 @user.find_voted_items.count 

 @user.get_voted(Comment).count ? 

 @user.comments.collect{|c| c.votes.size}.inject(:+) 

似乎没什么用。 我猜它与commontator gem处理代理关联关系的方式有关。 如何呈现特定用户在所有评论中收到的总票数? 很感谢任何forms的帮助!

编辑:我确实运行了所有迁移。

你不应该对评论进行投票吗? 用户模型acts_as_voter所以,根据gem上的文档,您可以使用find_voted_items检索用户投票的项目列表,但是注释模型是您可以计算votes因为这是用户投票的内容。

编辑,给出评论。 最简单的,你可能需要类似的东西:

 sum = 0 @user.comments.each do |comment| sum += comment.votes.count end 

虽然你可以通过inject甚至使用精心构造的“where子句”在投票字段上使用Activerecord#sum来更加雄辩。