Forum: Ruby on Rails How to get RAILS_ROOT when writing a ruby utility in /lib

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.
395dc94c927dea6d953dce3fc9406071?d=identicon&s=25 Peter Piper (Guest)
on 2006-02-17 12:09
Hi, Im writing a small ruby utility which at the moment is in /lib of my
rails app. However I don't get access to var's like RAILS_ROOT, do I
need to add an include to get this (and possibily other) rails
functionality?

Is /lib the right place for this kind of app, its a single ruby class
which I call from the command line. Would a rake file be better for this
kind of thing? Can rake's run ruby code,, im guessing yes?

Many thanks, K.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-02-17 13:33
(Received via mailing list)
i put my utility scripts in the script directory.  for me, a utility
script
is something that i need to run at the command line or in cron (ex:
clearing
out old session files)

put this at the top of your script:

# load application environment
require File.dirname(__FILE__) + '/../config/environment'

this will give you access to the RAILS_ROOT constant.


you also might be interested in script/runner

Chris
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris Leech (Guest)
on 2006-02-17 15:18
Many thanks Chris!

Chris Hall wrote:
> i put my utility scripts in the script directory.  for me, a utility
> script
> is something that i need to run at the command line or in cron (ex:
> clearing
> out old session files)
>
> put this at the top of your script:
>
> # load application environment
> require File.dirname(__FILE__) + '/../config/environment'
>
> this will give you access to the RAILS_ROOT constant.
>
>
> you also might be interested in script/runner
>
> Chris
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-02-17 21:36
Chris Hall wrote:
>
> put this at the top of your script:
>
> # load application environment
> require File.dirname(__FILE__) + '/../config/environment'
>
> this will give you access to the RAILS_ROOT constant.
>
>
> you also might be interested in script/runner

Using script/runner still doesn't give access to RAILS_ROOT and other
config stuff (I kinda think it should), so the require is still
required.

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