Friday, February 22, 2013

How to create new environment type (RAILS_ENV) for Rails3

You know there are development, test and production environments on Ruby on Rails. This article shows you how to create new environment for your system.

We are currently working with:

  • Ruby: 1.9.2
  • Ruby on Rails: 3.0.5

How to refer environment name

You can refer environment name through Rails.env.

p Rails.env #=> "development"

Adding new environment

For example, you can add your new 'staging' environment by these steps. Follow these steps to add your new 'staging' environment.

  1. copy RAILS_HOME/config/environments/production.rb as RAILS_HOME/config/environmentsstaging.rb
  2. add staging section into database.yml
If you have any other configuration file, edit the configuration too.

Then, create database, do migration, and start your staging server.

$ rake db:create RAILS_ENV=staging
$ rake db:migrate RAILS_ENV=staging
$ rails server -e staging
Yes! You now have new 'staging' environment for your project.

gem setting

You can use different gems for your each environment. This situation, write your Gemfile as below.

group :development, :test do
  gem 'sqlite3-ruby'
end
group :staging, :production do
  gem 'mysql2', '0.2.7'
end
You can reject unnecessary gems for specified environment.
$ bundle install --without test development
see: Bundler : Using Groups

This article is translated from Japanese.

No comments: