# Rails from the command line

**URL:** <https://www.ruby-forum.com/t/rails-from-the-command-line/53398>\
**Category:** Rails\
**Created:** [February 3, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398 "2006-02-03T21:49:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 3, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/1 "2006-02-03T21:49:46Z")

</div>

I have controller that processes data in one table and puts results in  
another. Now that its’ working, I would like to be able to run it from  
the  
command line ( read: windows batch file). How can this be done?

It will be installed on a Windows box with InstantRails1.0, (if that  
matters).

I will be doing more processes like this, so this is a good opportunity  
to  
get it right.

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 3, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/2 "2006-02-03T21:59:02Z")

</div>

I think you can do that with the “runner” script that rails creates for  
you in the script directory of your project. It  
uses the environment created by rails, so you can say something like  
“ruby script\runner MyClass.do\_something(“myarg”)”.

This wiki page mentions it at the bottom… there are probably others:

[http://wiki.rubyonrails.com/rails/pages/Environments#script](http://wiki.rubyonrails.com/rails/pages/Environments#script)

b

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 4, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/3 "2006-02-04T13:43:38Z")

</div>

Larry,

like Ben says you can use “$RAILS\_ROOT/script/runner” to invoke ruby  
code from the command line, which will run in a full Rails  
environment.

I would additionally suggest that you factor your code out of the  
controller. Controllers are fairly specific to their role in a web  
framework. They expect to be run in the context of a request that  
arrived via the web.

So depending on what exactly your code does, I would put it in a model  
or a lib file. Then you can call that code from your controller, and  
via script/runner.

cheers  
Gerret

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 4, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/4 "2006-02-04T14:41:41Z")

</div>

Hi Larry – the model files should contain, well, model-specific code,  
while the ‘/lib’ location holds libraries specific to your application  
that are not related to a particular model. You write that your code  
takes “data in one table and puts results in another”, so without  
knowing the details, I would guess you write some sort of “export” or  
“convert” function in your source model class.

cheers  
Gerret

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 4, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/5 "2006-02-04T14:05:15Z")

</div>

Thanks, Ben and Gerret!

I was thinking that the controller was probably not the best place for  
code  
that doesn’t need to call a web page. Just wasn’t sure was is should  
go.  
Thanks for that tip, as well. Why would I choose a lib location over a  
model?  
-Larry

On 2/4/06, Gerret A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arrived via the web.  
> you in the script directory of your project. It
> 
> > > I have controller that processes data in one table and puts results
> > > 
> > > * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 5, 2006, 2:15am UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/6 "2006-02-05T02:15:14Z")

</div>

Actually, the completed app will read data from DB2 and MySQL databases  
and  
store them in a local MySQL database after processing.

On 2/4/06, Gerret A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi Larry – the model files should contain, well, model-specific code,

> > Thanks, Ben and Gerret!  
> > On 2/4/06, Gerret A. \<removed\_email\_address@domain.invalid \> wrote:
> > 
> > > 
> > 
> > you in the script directory of your project. It
> > 
> > > >
> 
> that
> 
> > > > > ```
> > > > > --- E.Taft Benson
> > > > > 
> > > > > ```
> > > > 
> > > > * * *
> > 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [February 4, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/rails-from-the-command-line/53398/7 "2006-02-04T21:52:00Z")

</div>

This seems a little weird to me… shouldn’t /lib contain stuff that’s  
not  
application-dependent?

If so, code that knows how to move stuff around in the current  
application would be  
application dependent. Seems like rails needs an “/etc” or “/util”  
directory under “/app”.  
Or maybe db manipulation stuff could be considered not part of the  
app… but tied to the  
app…

Hmm, or maybe I’m over-thinking this… 🙂

b
