Forum: Ruby on Rails How to use script/runner

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Richard Williams (Guest)
on 2006-05-29 15:46
(Received via mailing list)
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?
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-29 17:14
(Received via mailing list)
On May 29, 2006, at 09:44 AM, Richard Williams 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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Richard Williams (Guest)
on 2006-05-29 21:36
(Received via mailing list)
On Monday, May 29, 2006, at 11:11 AM, Brian Hughes 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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails

Thank you for the clear explanation.

Richard
This topic is locked and can not be replied to.