I have a rails 4 app with a cron job that runs the following task once a day:
我有一个rails 4应用程序,它有cron作业,每天运行以下任务一次:
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?
我试过用条件来替换,但是我得到了相同的错误。什么好主意吗?
2 个解决方案
#1
4
Write your loop like this:
这样写你的循环:
@all_users.each do |user|
@packs = user.default_packs
if user.packs.where(:created_at => (Time.now.beginning_of_day..Time.now).exists?
puts "ERROR: User already has a record today."
else
user.packs.create(:amount => @packs)
end
end
I would however strongly suggest adding a scope to your Pack
model:
然而,我强烈建议在您的包模型中添加一个范围:
class Pack < ActiveRecord::Base
scope :today, -> { where(:created_at => (Time.now.beginning_of_day..Time.now)) }
end
Then your task would look like:
然后你的任务会是:
@all_users.each do |user|
@packs = user.default_packs
if user.packs.today.exists?
puts "ERROR: User already has a record today."
else
user.packs.create(:amount => @packs)
end
end
#2
4
You need to call where
on the class you're querying. (eg. User.where(...)
)
您需要调用正在查询的类的哪个位置。(如。User.where(…))
For example, to iterate over all the users created today, you'd do:
例如,要遍历今天创建的所有用户,您应该这样做:
task :update_packs => :environment do
User.where(:created_at => (Time.now.beginning_of_day..Time.now)).each do |user|
...
end
end
#1
4
Write your loop like this:
这样写你的循环:
@all_users.each do |user|
@packs = user.default_packs
if user.packs.where(:created_at => (Time.now.beginning_of_day..Time.now).exists?
puts "ERROR: User already has a record today."
else
user.packs.create(:amount => @packs)
end
end
I would however strongly suggest adding a scope to your Pack
model:
然而,我强烈建议在您的包模型中添加一个范围:
class Pack < ActiveRecord::Base
scope :today, -> { where(:created_at => (Time.now.beginning_of_day..Time.now)) }
end
Then your task would look like:
然后你的任务会是:
@all_users.each do |user|
@packs = user.default_packs
if user.packs.today.exists?
puts "ERROR: User already has a record today."
else
user.packs.create(:amount => @packs)
end
end
#2
4
You need to call where
on the class you're querying. (eg. User.where(...)
)
您需要调用正在查询的类的哪个位置。(如。User.where(…))
For example, to iterate over all the users created today, you'd do:
例如,要遍历今天创建的所有用户,您应该这样做:
task :update_packs => :environment do
User.where(:created_at => (Time.now.beginning_of_day..Time.now)).each do |user|
...
end
end