# How to get RAILS\_ROOT when writing a ruby utility in /lib

**URL:** <https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639>\
**Category:** Rails\
**Created:** [February 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639 "2006-02-17T12:09:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [February 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639/1 "2006-02-17T12:09:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [February 17, 2006, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639/2 "2006-02-17T13:33:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [February 17, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639/3 "2006-02-17T15:18:30Z")

</div>

Many thanks Chris!

Chris H. 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

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [February 17, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/how-to-get-rails-root-when-writing-a-ruby-utility-in-lib/69639/4 "2006-02-17T21:36:03Z")

</div>

Chris H. 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
