Rails from the command line


#1

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


#2

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

b


#3

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


#4

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


#5

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 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


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


#6

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 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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


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


#7

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… :slight_smile:

b