Ruby on rails 如何在活动记录信誉系统中聚合非主信誉
编辑注释:这是关于 我有一个应用程序,其中Ruby on rails 如何在活动记录信誉系统中聚合非主信誉,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,编辑注释:这是关于 我有一个应用程序,其中Vip和paun类的用户可以投票选择他们最喜欢的Idea对象。我能够成功收集贵宾票和牡丹票。如何将这两种投票类型聚合为总投票数?我有下面的代码,但是总投票数没有像预期的那样累积 idea.rb has_reputation :vip_votes, :source => :vip, :aggregated_by => :sum has_reputation :peon_votes
Vip
和paun
类的用户可以投票选择他们最喜欢的Idea
对象。我能够成功收集贵宾票
和牡丹票
。如何将这两种投票类型聚合为总投票数
?我有下面的代码,但是总投票数没有像预期的那样累积
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
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
resources :ideas do
member { post :vip_vote }
member { post :peon_vote }
end
def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end
ideas\u controller.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
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
resources :ideas do
member { post :vip_vote }
member { post :peon_vote }
end
def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end
routes.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
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
resources :ideas do
member { post :vip_vote }
member { post :peon_vote }
end
def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end
谢谢你的反馈 无法解决这个问题,我求助于将两种组合投票类型实际添加到控制器中的实例变量中 我在Idea#Show view文件中使用了total_投票,因此我添加了以下内容 ideas\u controller.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
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
resources :ideas do
member { post :vip_vote }
member { post :peon_vote }
end
def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end
不过,如果有人知道更好的方法,我将不胜感激。无法解决这个问题,我求助于将两种组合投票类型实际添加到控制器中的实例变量中 我在Idea#Show view文件中使用了total_投票,因此我添加了以下内容 ideas\u controller.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
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
resources :ideas do
member { post :vip_vote }
member { post :peon_vote }
end
def show
@total_votes = @idea.reputation_value_for(:vip_votes).to_i + @idea.reputation_value_for(:peon_votes) * 0.8
end
不过,如果有人知道更好的方法,我将不胜感激。对你来说可能太晚了,希望这能帮助其他人。我认为您要查找的代码可以在activerecord声誉系统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
还没有测试过,但希望有帮助…对你来说可能太晚了,希望这会帮助其他人。我认为您要查找的代码可以在activerecord声誉系统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
还没有测试过它,但如果paun.rb有这样定义声誉的“pains”表,希望能帮助…
has_reputation :votes, source: :user, aggregated_by: :sum
那么你应该在Idea.rb中为牡丹投票做这件事
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
如果paun.rb有这样定义声誉的“paun”表
has_reputation :votes, source: :user, aggregated_by: :sum
那么你应该在Idea.rb中为牡丹投票做这件事
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
如果您共享用于声誉的库,这将很有帮助,我认为该库提供了
has\u reputation
类方法和add\u evaluation
instance methodHi@rossta。说得好。我已经在帖子顶部添加了ActiveRecord声誉系统gem的链接。与您预期的相比,您从总投票数中看到的典型结果是什么?我什么也看不到。我可以通过ideas#vip#u vote添加vip#u vote,牡丹#u vote也可以添加vip#u vote。但是,这些投票不包括在总投票中。我尝试为总投票数添加一个定义,但这似乎也不起作用。如果您共享用于声誉的库,这将非常有用,我认为该库提供了has_声誉
类方法和add_evaluation
instance methodHi@rossta。说得好。我已经在帖子顶部添加了ActiveRecord声誉系统gem的链接。与您预期的相比,您从总投票数中看到的典型结果是什么?我什么也看不到。我可以通过ideas#vip#u vote添加vip#u vote,牡丹#u vote也可以添加vip#u vote。但是,这些投票不包括在总投票中。我曾尝试为总投票数添加一个定义,但这似乎也不起作用。我有一个类似的问题,我似乎无法回避。我有一个类似的问题,我似乎无法回避。