real nice framework you got here

Jared Carroll

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 files.

config/environment.rb on the other hand is loaded and executed for every environment.

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: do |config|
  # whole bunch of Rails config code

So if i defined my constant before that I’d be fine:

CSV_DIRECTORY = File.join RAILS_ROOT, 'lib', 'csv' do |config|
  # whole bunch of Rails config code

That’s terrible.

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

Jared Carroll

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.