Rails Rake 任务出错,而控制台等效任务则不会

发布于 2024-08-06 09:22:43 字数 694 浏览 2 评论 0原文

我有一段代码在控制台中运行良好,但在 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 技术交流群。

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

发布评论

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

评论(3

往事随风而去 2024-08-13 09:22:43

我遇到了同样的问题。我能让它工作的唯一方法是重新加载导致错误的类。

task :my_task => :environment do
  load "#{Rails.root}/app/models/my_class.rb" # Needed to do this

  foo = MyClass.create(my_attr: 'bar') # my_attr will be nil (huh??) unless I load the class
  foo.items.create(...) # This is the has_many that failed before loading the class
end

这就好像类加载器加载了类名,但没有得到任何定义。

I had this exact same problem. The only way I could get it to work was to reload the class that caused the error.

task :my_task => :environment do
  load "#{Rails.root}/app/models/my_class.rb" # Needed to do this

  foo = MyClass.create(my_attr: 'bar') # my_attr will be nil (huh??) unless I load the class
  foo.items.create(...) # This is the has_many that failed before loading the class
end

It's like the class loader loaded the class name, but it didn't get any of the definition.

自此以后,行同陌路 2024-08-13 09:22:43

在您的控制台中,Rails 环境已为您加载。

我希望您在创建rails任务时已经加载了rails环境。

更新

“RAILS_ENV = Production”

仅指定您正在使用的环境,仅此而已,因此您可以在代码中将其与“ENV [“RAILS_ENV”]”一起使用。

要加载导轨,您需要执行此操作。

  require File.dirname(__FILE__) + "/../config/environment" 

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" 
护你周全 2024-08-13 09:22:43

您是否告诉 rake 您的任务依赖于加载 Rails 环境?

namespace :scheduled do
 task :update_recurring => :environment do
   ...
 end 
end

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

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