views:

324

answers:

2

I have a piece of code that works fine in the console, but in a cron-driven rake task, it errors out every time, saying that the accessor function for one of the has_many relationships is not a valid method. Example:

provider has_many instances, so I'm calling provider.instances, and the rake task will throw back an error:

"undefined method `instances' for "#<Provider:0x7fff1c18a5d8>":Provider"

In the console, the same function pasted in works fine.

The rake call:

rake RAILS_ENV=production scheduled:update_recurring --trace

The console initialization:

script/console production

Rails version 2.3.2

See anything obvious?

UPDATE: The rake file is setup as so:

namespace :scheduled do
    task :update_recurring => :environment do
        Stuff that worked in console but not rake here
    end
end
A: 

In your console, the rails environment is loaded for you.

I am hoping that you have loaded the rails environment when you created the rails task.

Update

"RAILS_ENV=production"

just specifies the that which environment you are using thats all,so you can use it with "ENV["RAILS_ENV"]" inside your code.

to load rails you need to do this.

  require File.dirname(__FILE__) + "/../config/environment" 
Rishav Rastogi
RAILS_ENV=production specifies that rake load the production rails environment, right?
KordianGnot
just updated my answer.try that
Rishav Rastogi
I think the environment is loading properly, the other ActiveRecord relationships are working.
KordianGnot
Yeah, I just tried it, no luck, same issue. Thanks, though.
KordianGnot
A: 

Did you tell rake that your task is dependent on loading the Rails environment?

namespace :scheduled do
 task :update_recurring => :environment do
   ...
 end 
end
Milan Novota
Yeah, that's how it's setup verbatimnamespace :scheduled dotask :update_recurring => :environment docode that worked in console, but not in rake hereendend
KordianGnot