Ruby on rails 查找其他用户在帖子上收到的用户总投票数

Ruby on rails 查找其他用户在帖子上收到的用户总投票数,ruby-on-rails,ruby,ruby-on-rails-4,gem,Ruby On Rails,Ruby,Ruby On Rails 4,Gem,我用这个宝石来评论: 它的设置可轻松插入此gem,以便对评论进行投票: 我使用的是rails 4 btw,它与两个gems都兼容 在我的用户模型中: class User < ActiveRecord::Base acts_as_voter acts_as_commentator has_many :comments end 我试过: @user.find_voted_items.count 及 及 似乎什么都不管用。我猜这与commontator gem处理代理关联关系的

我用这个宝石来评论:

它的设置可轻松插入此gem,以便对评论进行投票:

我使用的是rails 4 btw,它与两个gems都兼容

在我的用户模型中:

class User < ActiveRecord::Base
  acts_as_voter
  acts_as_commentator
  has_many :comments
end
我试过:

@user.find_voted_items.count

似乎什么都不管用。我猜这与commontator gem处理代理关联关系的方式有关。如何呈现特定用户在所有评论上收到的投票总数?非常感谢您的帮助


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

你不应该对评论计票吗?用户模型充当投票人,因此,根据gem上的文档,您可以使用find_voted_items检索用户投票的项目列表,但评论模型是您可以计算投票的模型,因为用户正在投票

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

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

尽管使用inject,甚至使用Activerecordsum,在投票字段上使用精心构造的where子句,您可能会使这一点更加雄辩。

您运行迁移了吗?是的!我应该提到的!在CommonStator gem页面中,有一节描述了如何与acts_as_votable集成。你检查过那部分了吗。似乎你必须添加该设置,然后重新启动你的应用程序。@KatieHeidmann我想知道用户模型中的注释是否导致了这一切。当你在你的模型中加入演员兼评论员时,它已经为你增加了关系。我试图呈现特定用户所做的所有评论的总分。有点像他们的因果报应。我应该用什么方法?我只看过宝石的文档,但我认为您可能希望获得用户所做的评论的集合,然后对它们进行迭代以获得每个评论的分数。这给了我:SQLite3::SQLException:没有这样的列:commontator_comments.commontator_id:选择commontator_comments.*来自commontator_comments,其中commontator_comments.commontator_id=?commontator_comments.commontator_type=?好吧-好吧,在没有看到更多代码的情况下调试有点棘手-schema.rb文件中有什么,或者如果您在github上有项目,请向我们指出。。。。
undefined method `votes' for #<User:0x0000010dbf23a0>
<%= @user.comments.map{|c| c.votes.count}.inject(:+) %>
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(:+)
sum = 0
@user.comments.each do |comment|
    sum += comment.votes.count
end