我想知道如何访问模型属性,然后使用一些方法在 rake 任务中运行,从我读过的内容中,必须在任务之外声明方法,但是获得对模型的访问权让我很吃惊
我知道如果我把这个
namespace :grab do
task :scores => :environment do
puts User.all.inspect
end
end
然后我会打印所有用户
以下是我想要实现的目标
耙任务
namespace :grab do
task :scores => :environment do
points_total
allocate_points
end
end
def points_total
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.update_attributes!(score: points_total)
end
所以我需要访问我的预测和结果模型来执行这些方法......
任何帮助表示赞赏
谢谢
编辑
好的,所以运行上面的任务会给我以下错误
rake aborted!
undefined method `home_score' for #<Class:0x4b651c0>
还要更新这里是我的模型
class Prediction < ActiveRecord::Base
attr_accessible :away_score, :away_team, :fixture_id, :home_score, :home_team, :score
has_one :fixture
end
class Result < ActiveRecord::Base
attr_accessible :away_score, :away_team, :fixture_date, :home_score, :home_team
end