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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Clever Neologism
2008-Apr-16 16:19 UTC
Re: QUICK QUESTION: Run a Ruby File inside of Model
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 Peck <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---