2014-02-13 26 views
1

我用這種寶石的評論:https://github.com/lml/commontator渲染用戶計票

這是建立方便地插入這塊寶石對評論進行投票: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" = ? 

如何呈現特定用戶在所有評論中收到的總票數?

+0

安裝commentatoracts_as_votable寶石

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

後以下你試過'@ user.find_voted_items.count'還是'@ user.get_voted(Comment).count'? – usha

+0

是的,都給了我一個NoMethodError –

+1

你添加'acts_as_voter'用戶模型? – usha

回答

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 

,你應該能夠得到的票數這樣

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

現在我得到「無法找到表」評論「 –

+0

我真的很感謝您的迴應和努力,我只是不確定爲什麼獲得投票計數是不可能的。 –