I have a rails 4 app with a cron job that runs the following task once a day:
task :update_packs => :environment do
@all_users = User.all
@all_users.each do |user|
today = where(:created_at => (Time.now.beginning_of_day..Time.now))
@packs = user.default_packs
if user.packs.today.count >= 1
puts "ERROR: User already has a record today."
else
user.packs.create(:amount => @packs)
end
end
end
However, when I run the task, I get the following error:
NoMethodError: undefined method `where' for main:Object
/home/ben/rails_projects/hartwig/lib/tasks/packs.rake:7:in `block (2 levels) in <top (required)>'
/home/ben/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
/home/ben/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
/home/ben/rails_projects/hartwig/lib/tasks/packs.rake:6:in `block in <top (required)>'
/home/ben/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
/home/ben/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => update_packs
I've tried to replace where with conditions, but I get the same error. Any ideas?