views:

45

answers:

4

Can I do the following?

def ModelObserver < ActiveRecord

   def after_save
     Rake::Task[name].invoke
   end

end

At the moment, this returns the following error:

Don't know how to build task 'name'

Any idea?

+2  A: 

Use the system command :

def ModelObserver < ActiveRecord

   def after_save
     system "rake #{name}"
   end

end
shingara
This is probably slow as hell as it spawns the complete rails stack. You don't want to do that in an observer... Especially you REALLY don't want to do that in an after_save hook...
hurikhan77
auralbee
A: 

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

hurikhan77
+2  A: 

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.

samuil