雷克流产了!未定义的方法“jobs_to_be_started”对于主要:对象

发布于 2024-12-29 10:59:25 字数 437 浏览 0 评论 0原文

这是我试图运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2025-01-05 10:59:25

也许您错过了迭代器(例如 each)?

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.each do |job|
     job.status = "Running"     
     job.save
   end
end

而且您可能会在 job.saved 上收到错误,这是打印错误吗?我建议您在这里使用 update_attributes ,例如

jobs_to_be_started.each do |job|
  job.update_attributes :status => "Running"
end

Probably you've missed an iterator (each for example)?

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.each do |job|
     job.status = "Running"     
     job.save
   end
end

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.each do |job|
  job.update_attributes :status => "Running"
end
谁人与我共长歌 2025-01-05 10:59:25

示例中的以下代码是对方法 jobs_to_be_started 的调用:

jobs_to_be_started do |job|
  job.status = "Running"     
  job.saved # If you don't define `saved` method yourself it should be `save`
end

只要方法始终在对象上调用并且您没有指定对象,则在 <代码>自我。在您的情况下, self 是主要的 Object (代码外部类/模块定义的上下文)。
主对象未定义 jobs_to_be_started 方法,这就是您收到此错误的原因。

从您的代码中,我假设您希望调用 jobs_to_be_started 列表中的每个作业,因此您很可能想要执行如下操作:

jobs_to_be_started.each do |job|
  job.status = "Running"     
  job.saved
end

在这里,您调用 each 方法(并将此关联使用 jobs_to_be_started 对象的块调用。

The following code from your example is an invocation of the method jobs_to_be_started:

jobs_to_be_started do |job|
  job.status = "Running"     
  job.saved # If you don't define `saved` method yourself it should be `save`
end

As far as methods are always invoked on objects and you did not specified an object, jobs_to_be_started is invoked on self. In your case self is the main Object (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:

jobs_to_be_started.each do |job|
  job.status = "Running"     
  job.saved
end

Here you call each method (and associate this call with the block) of jobs_to_be_started object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文