# QUICK QUESTION: Run a Ruby File inside of Model

**URL:** <https://www.ruby-forum.com/t/quick-question-run-a-ruby-file-inside-of-model/134960>\
**Category:** Rails\
**Created:** [April 16, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/quick-question-run-a-ruby-file-inside-of-model/134960 "2008-04-16T16:54:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [April 16, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/quick-question-run-a-ruby-file-inside-of-model/134960/1 "2008-04-16T16:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [April 16, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/quick-question-run-a-ruby-file-inside-of-model/134960/2 "2008-04-16T17:11:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [April 16, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/quick-question-run-a-ruby-file-inside-of-model/134960/3 "2008-04-16T18:20:10Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
