I was wondering how to access a Models attributes and then run in a rake task using some methods, from what i have read methods have to be declared outside of the task, but gaining access to the model is throwing me
I know that if i put this
namespace :grab do
task :scores => :environment do
puts User.all.inspect
end
end
I would then have all the users printed
Below is what I am trying to achieve
Rake Task
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
So i need access to my Prediction and Result model to perform these methods...
Any help appreciated
Thanks
EDIT
ok so running the task as it is above gives me the following error
rake aborted!
undefined method `home_score' for #<Class:0x4b651c0>
also to update here are my models
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