渲染用户投票计数

时间:2014-02-13 21:34:51

标签: ruby-on-rails ruby gem

我正在使用此宝石评论:https://github.com/lml/commontator

设置为轻松插入此gem以对评论进行投票:https://github.com/ryanto/acts_as_votable

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

<%= @user.votes.count %>

我收到此错误

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" = ?

如何呈现特定用户在所有评论中收到的总投票数?

1 个答案:

答案 0 :(得分:2)

假设您有此设置

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

安装commentatoracts_as_votable宝石后的以下内容

rails generate acts_as_votable:migration
rake commontator:install
rake db:migrate

你应该能够得到像这样的票数

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