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


def update_things delete_cache
things = Thing.find(:all, :conditions => “updated = false”)

Run an updater in “lib/updater/do_update.rb”

What goes on this line? # ruby script/runner do_update.rb

end

Sorry, the above thing should say:

Thing.rb


def update_things delete_cache
things = Thing.find(:all, :conditions => “updated = false”)

Run an updater in “lib/do_update.rb”

What goes on this line? # ruby do_update.rb

end

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 ;).

On Apr 16, 11:11 am, Joe P. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs