Here’s a good one I ran into in Rails the other day.
So the files under the
config/environments directory are environment specific
config/environment.rb on the other hand is loaded and executed for every
What I wanted was a constant that was the same in the development and production environments but different in the test environment. So I naturally put the test environment specific value in
CSV_DIRECTORY = File.join RAILS_ROOT, 'test', 'csv'
And instead of putting the development and production specific value in their specific config files I put it in:
CSV_DIRECTORY = File.join RAILS_ROOT, 'lib', 'csv'
That way its only in one place.
Now when I run my tests or ‘
ruby script/console test’ I expect:
CSV_DIRECTORY => '/path/to/my/app/test/csv'
But instead I get:
CSV_DIRECTORY => '/path/to/my/app/lib/csv'
What’s going on here?
You’re telling me the generic environment config file gets executed after the environment specific config file?
It turns out half of it does. Everything above the following code gets executed before the current environment’s specific config file:
Rails::Initializer.run do |config| # whole bunch of Rails config code end
So if i defined my constant before that I’d be fine:
CSV_DIRECTORY = File.join RAILS_ROOT, 'lib', 'csv' Rails::Initializer.run do |config| # whole bunch of Rails config code end
I also like the fact that further down
config/environment.rb after the call to
Rails::Initializer#run there’s the following comment:
# Include your application configuration below
I’m going to go use a real man’s framework