Rails application directory.

Hi all,

I was wondering if there was a kosher way of getting the complete path to the root location of a Rails app? I know there's ROOT_PATH, but I think that changes when I do Dir.chdir() to some other location. Or at least it's what I am seeing.

For example:


=> "/Users/diego/Code/Rails/mview"


script/../config/../config/.. => nil


=> 0


script/../config/../config/.. => nil


=> "/"

So basically anytime I do a chdir, away from the Rails root dir, I can no longer use RAILS_ROOT to get a starting point to do file and directory work.

Do I just need to define a global const with my app's complete path and use that?

Cheers, Diego

I suppose it depends on what context you need the root for. One way that always seems to work is to ask for File.dir(__FILE__)


Sorry. File.dirname(__FILE__)

Thanks, Scott. I want the root as a starting point from which I can (for example) store uploaded files somewhere in my application's directory structure. I may want to get to:


So I wanted to have a way to always get /Users/diego/MyApp/ and I can go off from there.

Thanks for the tip on using __FILE__. What I did, before your reply, was to add APP_ROOT = File.expand_path(RAILS_ROOT) in the application's initialization.