QUICK QUESTION: Run a Ruby File inside of Model

Hi,

I'm trying to run a ruby file in lib from inside of a model. How can I run a ruby file from inside of a model?

In this example, Thing.rb is my model. I'm trying to run do_update.rb. How can I call this from inside of Thing.rb?

Thing.rb

Sorry, the above thing should say:

If you don't need to pass too many params, system("path/to/script.rb #{param1}....") will work (assuming it's sh-banged). If you want it to be asynchronous (i.e. Rails starts the script then keeps going, and doesn't care how long the script takes over to the side), I would suggest deamonizing your script and have it poll a database every X secs to see if an action is requested, do it's thing, and mark the task as complete or remove it from the table. This also allows you to off-load and parallelize (if you use record locking and multiple daemons) other compute-intensive tasks from Rails. Your Thing.rb model would simply set a flag or insert a record into some queue table that the script polls. Using Active Record outside of rails, this is a snap.

If these two conditions aren't true, turn your script into a Module with methods and use it as a mixin to the model classes that need to call it. Or just copy and paste the code ;).