当我为使用插件中定义的模型的应用程序运行 rake 任务时,我得到一个未初始化常量错误,但是当我使用脚本/运行器运行模型进程时,它在 rake 任务中被触发,那么作业运行正常吗?
加载我所有插件的脚本/运行程序之间是否存在一些区别,即使它正在传递一个环境,当我启动一个 rake 任务时也不会发生这种情况?
当我为使用插件中定义的模型的应用程序运行 rake 任务时,我得到一个未初始化常量错误,但是当我使用脚本/运行器运行模型进程时,它在 rake 任务中被触发,那么作业运行正常吗?
加载我所有插件的脚本/运行程序之间是否存在一些区别,即使它正在传递一个环境,当我启动一个 rake 任务时也不会发生这种情况?
您的 rake 任务需要依赖于 :environment。这将启动您的应用程序的环境并让您访问您的模型等。
例如
desc "Make DB Views"
task :views => [:environment] do |t|
# your task's code
end
You need to specify that your Rake task requires the environment to be loaded:
task :your_task => :environment do |t| ...
or
task :your_task => [:environment] do |t| ...
or
task :your_task, :param1, :param2, :needs => :environment do |t, args| ...
or
task :your_task, :param1, :param2, :needs => [:environment] do |t, args| ...
If you did specify this, then there is another problem. I think one common source of errors is due to the fact that the plugins are loaded inside a namespace called Rails::Plugin
. So if you defined a class called Foo
in your plugin, then the Rake task needs to reference it as Rails::Plugin::Foo
instead of simply Foo
.
If this does not solve your problem then try to add puts "Check"
on the first line of the plugin's init.rb
file, and make sure that Check
is displayed when you run your rake task. If it is, then your plugin is being loaded, but perhaps it fails silently after that.
One last thing: maybe you are trying to use the plugin outside the task, for example at the beginning of your Rake file, in some initialization code? If so, then it will fail because the plugins only get loaded when the task is executed (when the environment is loaded).
Hope this helps.