Are plugins available for the rake task?

When I run a rake task for an application using the Models defined in the plugin, I get an Uninitialized Constant error, but when I start the model process, with a script / runner that runs in the rake task, does it work fine?

Is there a difference between a script / runner that loads all my plugins that doesn't happen when I run the rake task, even if it is passed to the environment?

0


a source to share


2 answers


Your rake challenge should depend on: environment. This will expand your application environment and give you access to your models, etc.

For instance,



desc "Make DB Views"
task :views => [:environment] do |t|
# your task code

end

      

+1


a source


You need to indicate that your Rake task requires the environment to load:

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 specify this, another problem arises. I think one common source of error has to do with plugins being loaded inside a namespace called Rails::Plugin

. Therefore, if your plugin defines a class with a name Foo

, the Rake task should refer to it as Rails::Plugin::Foo

instead of simple Foo

.

If that doesn't solve your problem, try adding puts "Check"

to the first line of the plugin file init.rb

and make sure it Check

shows up when you run your rake command. If so, your plugin is loading, but it may fail after that.

One last thing: maybe you're trying to use the plugin outside of a task, like at the beginning of your Rake file, in some initialization code? If so, then this will happen because the plugins will only be loaded when the task is executed (when the environment is loaded).

Hope it helps.

0


a source







All Articles