开发者

Invoke rake task from ActiveRecord observer

开发者 https://www.devze.com 2023-01-02 22:34 出处:网络
Can I do the following? def ModelObserver < ActiveRecord def after_save Rake::T开发者_JAVA技巧ask[name].invoke

Can I do the following?

def ModelObserver < ActiveRecord

   def after_save
     Rake::T开发者_JAVA技巧ask[name].invoke
   end

end

At the moment, this returns the following error:

Don't know how to build task 'name'

Any idea?


Use the system command :

def ModelObserver < ActiveRecord

   def after_save
     system "rake #{name}"
   end

end


Consider using delayed job or similar plugin to handle background execution. In observer (or controller) just notify background job daemon, that it should take care of some action, instead of running this task directly.


In Rails3 if you still want to call rake task like this:

Rake::Task[name].invoke

you have to put

[Application].load_tasks

before invoke command, where application is your application name. For example I had to put

Ead::Application.load_tasks


I suppose you have to load the Rake environment first, and the Rakefile. I would not try to invoke the fullblown command line to do that. You probably need to use "import" as can be found in the Rake API

0

精彩评论

暂无评论...
验证码 换一张
取 消