Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

real nice framework you got here

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

config/environments/test.rb

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:

config/environment.rb

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:

1
2
3
Rails::Initializer.run do |config|
  # whole bunch of Rails config code
end

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

1
2
3
4
5
CSV_DIRECTORY = File.join RAILS_ROOT, 'lib', 'csv'

Rails::Initializer.run do |config|
  # whole bunch of Rails config code
end

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


About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.