Rails - 如何访问/增加此变量的值?

时间:2016-08-28 16:31:14

标签: ruby-on-rails variables models

对不起,如果之前已经出现过 - 我保证我已经尝试过找到它。

我的rails应用程序将用户的足球得分预测与实际足球得分进行比较。如果结果是正确的(例如巴塞罗那胜利),你得到1分。如果结果和得分是正确的(巴塞罗那赢3-0),你得到3分。

' if'逻辑等都很好,但我很难获得“积分”。记录在我的用户模型中。以下是我在控制器中的内容 - 任何想法应该在我为用户分配点的行中(下面突出显示)?

这只是一个摘录,索引是针对某些单独的东西,所以请不要因此而过多地避免 - 我只是试图找出替换两条突出显示的行的内容。欢迎所有建议!

预测控制器

def update scores
  Prediction.joins(:user).each_with_index do |p, :|
  if p.predictionarray[0] == Leagueresult.last.resultarray[0] &&   p.predictionarray[1..2] == Leagueresult.last.resultarray[1..2]
    User.points = User.points + 3 <<<<< What goes on this line?
  elsif p.predictionarray[0] == Leagueresult.last.resultarray[0] && p.predictionarray[1..2] != Leagueresult.last.resultarray[1..2]
    User.points = User.points + 1 <<<<< And this line?
  end
end

1 个答案:

答案 0 :(得分:0)

您正在尝试引用User - 模型,而不是User - 类对象。您正在寻找的用户对象似乎是p.user