2014-04-18 33 views
1

我在檢查用戶是否對給定的帖子進行投票,然後更改上下箭頭的樣式。 activerecord-reputation-system寶石尚未在一段時間內更新,我發現每個例子都使用過時的代碼,或者需要分別跟蹤「向上」投票和「向下」投票。我更喜歡使用:post_votes並檢查該參數的值。Activerecord聲望系統 - 如何檢查current_user的投票值

posts_controller.rb

@voted_items = Post.evaluated_by(:post_votes, current_user) 

index.html.erb

<% if current_user && @voted_items.include?(post) %> 
    # display active up or down arrows 
<% end %> 

我怎麼只是@voted_items對象中的相關記錄的價值?

編輯:我注意到沒有內置的範圍:value。也許手動添加一個會讓我查詢一個特定的值?

回答

0

現在我來到了該模型這種方法DEF:

def evaluation_value(user, comment) 
    if @up_voted = ReputationSystem::Evaluation.where(:reputation_name => "comment_votes", 
     :value => "1.0", :source_id => user.id, :source_type => user.class.name, 
     :target_id => comment.id, :target_type => comment.class.name).exists? 
    "upvoted" 
    elsif @down_voted = ReputationSystem::Evaluation.where(:reputation_name => "comment_votes", 
     :value => "-1.0", :source_id => user.id, :source_type => user.class.name, 
     :target_id => comment.id, :target_type => comment.class.name).exists? 
    "downvoted" 
    else 
    nil 
    end 
end 

,因爲它需要硬編碼值和其他細節它的效果並不理想,但直到寶石被更新,以使其適用於現在檢查個人價值。

這種方式我可以運行comment.evaluation_for(current_user, comment) == "upvoted"或downvoted。