# Place for special scripts in the framework?

**URL:** <https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570>\
**Category:** Rails\
**Created:** [September 15, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570 "2007-09-15T20:26:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 15, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/1 "2007-09-15T20:26:43Z")

</div>

Hey there,

I have a script which scrapes a website. Since it takes nearly an hour  
to complete, making it part of the controller doesn’t make sense because  
it would simply time out.

However, this script needs access to my models (it adds to my database).

How can I do this? I am sure there is something simple I am missing.

Thanks!

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 16, 2007, 2:46am UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/2 "2007-09-16T02:46:09Z")

</div>

Sy Ys wrote:

> Hey there,
> 
> I have a script which scrapes a website. Since it takes nearly an hour  
> to complete, making it part of the controller doesn’t make sense because  
> it would simply time out.
> 
> However, this script needs access to my models (it adds to my database).
> 
> How can I do this? I am sure there is something simple I am missing.
> 
> Thanks!

It depends exactly how it’s working.  
Often I end up defining a module and sticking it in /lib,  
but, equally, any associated model code should live in the model.

Then, in order to run it,  
either create a /bin directory  
or stick it in /script

inside your script, make sure you require “environment.rb” like this  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)  
then when you run your script (on cron or whatever), it’ll first load up  
the rails stack, giving you access to the db, models, and all that.

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 16, 2007, 2:48am UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/3 "2007-09-16T02:48:50Z")

</div>

Excellent reply. Thanks.

Matthew R. wrote:

> > stuff

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/4 "2007-09-17T13:24:22Z")

</div>

If you just want to manually run this, or run it from cron, you can use  
/script/runner and pass it the path to a script. It will execute the  
script within the rails environment so everything will be available to  
you. The script can be really simple if the methods are already defined  
in models or libraries. like:

In a model:

class Foo \< ActiveRecord::Base  
def self.do\_long\_foo\_thing  
do\_something\_long  
end  
end

Then in a script /tmp/my\_script:

Foo.do\_long\_foo\_thing

then /path/to/rails/app/script/runner /tmp/my\_script

I created a site\_scripts directory under my scripts directory to hold  
these, but you can put them wherever. I use them for cleaning sessions  
and various other tasks.

However, if this is something that needs to be initiated from a web  
page, but you want to background it, check out BackgroundDRB  
[http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/).

Hope this helps.

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/5 "2007-09-17T13:24:23Z")

</div>

Unless I’m reading it wrong or misunderstanding the question, the README  
file created by "rails ", recommends the lib directory.

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/6 "2007-09-25T23:09:13Z")

</div>

Exactly, I just assumed he had more to do than that. I was simply  
demonstrating that most of the time the script needs not includes, etc.  
This was obviously an example.

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/place-for-special-scripts-in-the-framework/114570/7 "2007-09-17T13:25:05Z")

</div>

> ```
> do_something_long
> 
> ```
> 
> end  
> end
> 
> Then in a script /tmp/my\_script:
> 
> Foo.do\_long\_foo\_thing

If that’s all you’re doing there’s no reason to make /tmp/my\_script.  
Just  
do like this:

script/runner ‘Foo.do\_long\_foo\_thing’
