如何在活动记录信誉系统中聚合非主要信誉

时间:2012-10-03 20:27:17

标签: ruby-on-rails activerecord

修改注意:这是关于activerecord-reputation-system gem

中涵盖的Railscast#364的问题

我有一个应用程序,VipPeon类的用户可以投票选出他们最喜欢的Idea对象。我能够成功收集vip_votespeon_votes。如何将两种投票类型汇总到total_votes?我有下面的代码,但total_votes没有按预期累积。

idea.rb

has_reputation :vip_votes, 
               :source => :vip,
               :aggregated_by => :sum 

has_reputation :peon_votes, 
               :source => :peon,
               :aggregated_by => :sum 

has_reputation :total_votes, 
               :source => [{ :reputation => :vip_votes },
                           { :reputation => :peon_votes, :weight => 0.8 }],
               :aggregated_by => :sum 

ideas_controller.rb

def vip_vote
  @design = Design.find(params[:id])
  @design.add_evaluation(:vip_votes, 1, current_vip)
end

def peon_vote
  @design = Design.find(params[:id])
  @design.add_evaluation(:peon_votes, 1, current_peon)
end

的routes.rb

resources :ideas do
  member { post :vip_vote }
  member { post :peon_vote }
end

感谢您的反馈!

3 个答案:

答案 0 :(得分:1)

无法解决问题,我将这两种组合投票类型实际添加到控制器中的实例变量中。

我在Idea#Show视图文件中使用了total_votes,因此我添加了以下内容。

<强> ideas_controller.rb

def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end

如果有人知道有更好的方法可以做到这一点,我们将不胜感激。

答案 1 :(得分:0)

对你来说可能为时已晚,希望能帮助别人。我认为您所使用的代码可以在Quick Start中找到,用于activerecord-reputation-system gem:

将所做的工作转移到您的问题上:

has_reputation :total_votes, 
               :source => [{ :reputation => :vip_votes },
                           { :reputation => :peon_votes, :weight => 0.8 }] # no comma here
               # this line seems to be assumed: :aggregated_by => :sum

没有测试过,但希望有帮助...

答案 2 :(得分:0)

如果Peon.rb有'peons'表,其声誉定义如下

has_reputation :votes, source: :user, aggregated_by: :sum

然后你应该在Idea.rb中为peon_votes做这个。

 has_reputation :peon_votes,
    :source => {reputation: :votes, of: :peons}  

同样适用于Vip.rb

然后计算Idea.rb中的总票数,你就这样做了

has_reputation :total_votes, 
               :source => [{ :reputation => :vip_votes },
                           { :reputation => :peon_votes, :weight => 0.8 }],
               :aggregated_by => :sum 
相关问题