How to use script/runner


#1

I’ve read the wiki and Agile and can’t find the answer.

When using script/runner I’ve gotten simple model functions similar to
the -help example to run:

$script/runner ‘puts Vendor.find(1).name3’

where Vendor is a model.

However when I try to run a function in a controller it doesn’t work:

$script/runner ‘puts ApplicationHelper.money(100)’
script/runner:29: undefined method `money’ for ApplicationHelper:Module
(NoMethodError)

I’m sure there is a simple solution to this.

More generally, does script/runner look in every class in the
application to try and find the referenced class?


#2

On May 29, 2006, at 09:44 AM, Richard W. wrote:

$script/runner ‘puts ApplicationHelper.money(100)’
script/runner:29: undefined method `money’ for
ApplicationHelper:Module
(NoMethodError)

I’m sure there is a simple solution to this.

You’ve actually got two separate problems here:

First, you say that you want to use script/runner to execute a
Controller method. But Controller methods are executed in the context
of an HTTP request, which script/runner doesn’t know how to set-up.
However, you could easily create a class, say inside the lib folder,
with a method that mimicked what your Controller action does, without
needing the HTTP request environment. Then you would be able to call
that method from script/runner. Also, make sure you prepend “self.”
onto the name of the method, since you won’t first be instantiating
an object of that class before you try to call the method.

Second, Helper methods aren’t actually part of your Controllers. They
are modules that get mixed in to the View rendering step, so that
your view templates can call them. Until they are included, their
methods really aren’t accessible, since they were never designed to
be called from outside the including View’s context.

More generally, does script/runner look in every class in the
application to try and find the referenced class?

When you execute script/runner from the command line you are actually
launching an instance of your Rails app. The first thing that ruby
does is load all of your application’s code into memory, which then
makes all of your classes, and their methods, available for you to
reference. However, that doesn’t mean that you can call all of them
from a command line context. As I’ve explained above, some methods
need to be invoked in certain ways in order for them to function
properly.

The script/runner program gives you a way to access the business
logic (this is the code that you add to your Model classes) from
outside the web/http context that they are normally accessed through.
It’s one of the best ways to run periodic routines that need to check
the data of your app (eg. expiring ActiveRecord session table
objects, based on their last modified time), as well as a great way
to batch run certain processes, like data imports and exports (this
is mostly how I use script/runner).

-Brian


#3

On Monday, May 29, 2006, at 11:11 AM, Brian H. wrote:

However when I try to run a function in a controller it doesn’t work:
Controller method. But Controller methods are executed in the
that your view templates can call them. Until they are included,
for you to reference. However, that doesn’t mean that you can call
as a great way to batch run certain processes, like data imports and
exports (this is mostly how I use script/runner).

-Brian


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

Thank you for the clear explanation.

Richard