[英]has_many association between models
I have a rake task that allocates a score when comparing a prediction and result, at the moment the relationship is a has_one, as in a prediction has_one :result.. 我有一个rake任务,当比较预测和结果时会分配分数,目前的关系是has_one,就像预测has_one:result一样。
I want to change this to a prediction has_many :results. 我想将其更改为预测has_many:results。 My task looks like this 我的任务看起来像这样
namespace :grab do
task :scores => :environment do
Prediction.all.each do |prediction|
score = points_total prediction, prediction.result
allocate_points prediction, score
end
end
end
def points_total(prediction, result)
wrong_predictions = [prediction.home_score - result.home_score, prediction.away_score - result.away_score]
wrong_predictions = wrong_predictions.reject { |i| i == 0 }.size # returns 0, 1 or 2
case wrong_predictions
when 0 then 3
when 1 then 1
else 0
end
end
def allocate_points(prediction, score)
prediction.update_attributes!(score: score)
end
At the moment when running the task i get the error 在运行任务的那一刻,我得到了错误
undefined method result for Prediction Class
So with the has_many relationship would i be able to access the result attributes via 所以有了has_many关系,我将能够通过访问结果属性
prediction.result.home_score
or am i getting confused somewhere. 还是我在某个地方感到困惑。 I dont know how to refactor my rake task to fit in with the new relationship 我不知道如何重构我的耙子任务以适应新的关系
Any advice appreciated 任何建议表示赞赏
EDIT 编辑
even after receiving the below advice from @andrunix cant seem to figure out how to apply to the rake task , this is what i have so far but get error undefined method to_i for array 即使在从@andrunix收到以下建议后,似乎也无法弄清楚如何应用到rake任务,这是我到目前为止所遇到的,但是对数组获取错误undefined方法to_i
namespace :grab do
task :scores => :environment do
Prediction.all.each do |prediction|
score = points_total prediction
allocate_points prediction, score
end
end
end
def points_total prediction
prediction.results.each do |result|
result_h = result.home_score
result_a = result.away_score
wrong_predictions = [prediction.home_score - result_h, prediction.away_score - result_a]
wrong_predictions = wrong_predictions.reject { |i| i == 0 }.size # returns 0, 1 or 2
case wrong_predictions
when 0 then 3
when 1 then 1
else 0
end
end
end
def allocate_points prediction, score
prediction.update_attributes!(score: score)
end
If a prediction :has_many results, then results is now a collection and you'll have to treat it as such. 如果预测:has_many结果,则结果现在是一个集合,您必须将其这样对待。 You can't say, 你不能说
prediction.result.home_score
You have to reference results with an array index like, 您必须使用数组索引引用结果,例如
prediction.results[0].home_score
Or iterate over the collection of results like: 或遍历以下结果的集合:
prediction.results.each do |result|
result.home_score # do something with the score
end
I realize this doesn't totally answer the "how to refactor" question but if a Prediction :has_many results, you can't simply reference prediction.result anymore. 我意识到这并不能完全回答“如何重构”问题,但是如果Prediction:has_many结果出现,您将无法再简单地引用预测。
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.