Custom Rails Environments

It’s slightly more involved than you might think to make a custom Rails environment that is based on another. In my case, I wanted to have a staging environment that was as close as possible to production. So, I thought require 'config/environment/production' should do the trick.

Not so.

Because of the config.foo magic and the fact that it requires binding tomfoolery, environments aren’t loaded, or loadable, with require. They’re read and eval’d. Here’s what I’ve got at the top of config/environment/staging.rb at the moment:


production_environment_path = File.join(File.dirname(configuration.environment_path), 'production.rb')
eval(IO.read(production_environment_path), binding, production_environment_path)

So far so good. I’ll update here if that turns out not to be the whole story.

Leave a Reply

Entries (RSS)