user841747
user841747

Reputation:

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?

Upvotes: 0

Views: 934

Answers (2)

Aliaksei Kliuchnikau
Aliaksei Kliuchnikau

Reputation: 13739

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.

Upvotes: 0

alony
alony

Reputation: 10823

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

Upvotes: 1

Related Questions