Rails Rake 任务出错,而控制台等效任务则不会
我有一段代码在控制台中运行良好,但在 cron 驱动的 rake 任务中,每次都会出错,表示 has_many 关系之一的访问器函数不是有效方法。示例:
provider has_many 实例,所以我调用provider.instances,rake 任务将抛出一个错误:
"undefined method `instances' for "#<Provider:0x7fff1c18a5d8>":Provider"
在控制台中,粘贴的相同函数工作正常。
rake 调用:
rake RAILS_ENV=production scheduled:update_recurring --trace
控制台初始化:
script/console production
Rails 版本 2.3.2
看到什么明显的东西了吗?
更新: rake 文件设置如下:
namespace :scheduled do
task :update_recurring => :environment do
Stuff that worked in console but not rake here
end
end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题。我能让它工作的唯一方法是重新加载导致错误的类。
这就好像类加载器加载了类名,但没有得到任何定义。
I had this exact same problem. The only way I could get it to work was to reload the class that caused the error.
It's like the class loader loaded the class name, but it didn't get any of the definition.
在您的控制台中,Rails 环境已为您加载。
我希望您在创建rails任务时已经加载了rails环境。
更新
“RAILS_ENV = Production”
仅指定您正在使用的环境,仅此而已,因此您可以在代码中将其与“ENV [“RAILS_ENV”]”一起使用。
要加载导轨,您需要执行此操作。
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.
您是否告诉 rake 您的任务依赖于加载 Rails 环境?
Did you tell rake that your task is dependent on loading the Rails environment?