【问题标题】:Activerecord-reputation-system - how to check value of current_user's voteActiverecord-reputation-system - 如何检查 current_user 的投票值
【发布时间】:2014-04-18 19:23:47
【问题描述】:

我正在检查用户是否对给定帖子进行了投票,然后更改了向上和向下箭头的样式。 activerecord-reputation-system gem 有一段时间没有更新了,我发现的每个示例要么使用过时的代码,要么需要分别跟踪“赞成”票和“反对”票。我更喜欢只使用:post_votes 并检查那个参数的值。

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 没有内置范围。也许手动添加一个可以让我查询特定值?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord ruby-on-rails-4


    【解决方案1】:

    现在我在模型上想出了这个方法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  
    

    这并不理想,因为它需要对值和其他细节进行硬编码,但它现在可以工作,直到 gem 更新以允许检查单个值。

    这样我可以运行comment.evaluation_for(current_user, comment) == "upvoted" 或投反对票。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多