雷克流产了!未定义的方法“jobs_to_be_started”对于主要:对象
这是我试图运行的 rake 任务
desc "This task changed the status of started jobs"
task :start_status => :environment do
jobs_to_be_started = Job.find_all_by_status("Started")
jobs_to_be_started do |job|
job.status = "Running"
job.saved
end
end
这是我收到的错误
Rake aborted! undefined method `jobs_to_be_started' for main:Object
谷歌搜索后看不到明显的答案,有人能指出我正确的方向吗?
This is the rake task I am trying to run
desc "This task changed the status of started jobs"
task :start_status => :environment do
jobs_to_be_started = Job.find_all_by_status("Started")
jobs_to_be_started do |job|
job.status = "Running"
job.saved
end
end
And this is the error I am receiving
Rake aborted! undefined method `jobs_to_be_started' for main:Object
Have had a google and can't see an obvious answer, can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您错过了迭代器(例如
each
)?而且您可能会在 job.saved 上收到错误,这是打印错误吗?我建议您在这里使用
update_attributes
,例如Probably you've missed an iterator (
each
for example)?And also you will probably get an error on job.saved, is that a misprint? i would suggest you to use
update_attributes
here, like示例中的以下代码是对方法
jobs_to_be_started
的调用:只要方法始终在对象上调用并且您没有指定对象,则在 <代码>自我。在您的情况下,
self
是主要的Object
(代码外部类/模块定义的上下文)。主对象未定义
jobs_to_be_started
方法,这就是您收到此错误的原因。从您的代码中,我假设您希望调用
jobs_to_be_started
列表中的每个作业,因此您很可能想要执行如下操作:在这里,您调用
each
方法(并将此关联使用jobs_to_be_started
对象的块调用。The following code from your example is an invocation of the method
jobs_to_be_started
:As far as methods are always invoked on objects and you did not specified an object,
jobs_to_be_started
is invoked onself
. In your caseself
is the mainObject
(context for code ouside class/module definition).Main Object does not define
jobs_to_be_started
method and this is why you get this error.From your code I assume that you expect to call each job in
jobs_to_be_started
list, so you most likely want to do something like this:Here you call
each
method (and associate this call with the block) ofjobs_to_be_started
object.