Ruby on Rails
Environments (Version #53)

Rails has the concept of environments to represent the stages of an application’s lifecycle: test, development, and production are the defaults. Specify your choice with the RAILS_ENV environment variable.

RAILS_ENV can currently have one of three values:
  • development
  • test
  • production

Within a Rails application, RAILS_ENV can most easily be accessed with the following code:

ENV["RAILS_ENV"]

Each environment has a corresponding environment file (eg. config/environments/name.rb) with its particular config code.

Loading the rails environment gives you a fully configured context for accessing the model in an Rails application. This is extremely useful when you need to run upgrade or maintenance scripts. Or when you need to interrogate the model ad-hoc.

From the “console” script

There’s a helper script that will load the irb and your rails environment, with no additional configuration required.

$ script/console production

That’ll start up a IRB session for the “production” environment with all the nice settings in there (auto-completion and no inspection). You can read more about how to use the console here . This can be a super convenient place to create some database records.

From a Ruby script

Maintenance script example (placed in scripts/):

RAILS_ENV = 'production'
require File.dirname(__FILE__) + '/../config/environment'

for account in Account.find(:all)
  account.recalculate_interests
end

This will bring up the model and provide logging to log/production.log.

From IRB

You can manually load your environment and models into a running IRB session. It’s easier just to run script/console as described above, though, and get exactly the same thing.

$ irb
irb(main):001:0> require 'config/environment'
=> true
irb(main):002:0> require 'post'
=> true
irb(main):003:0> post = Post.find(1)
=> #<Post:0x124694c @attributes={"title"=>"su", "body"=>"asdf -- that's nice asd  asd", 
"author"=>"david", "id"=>"1", "written_on"=>"2004-07-25 00:00:00"}>
irb(main):004:0> post.title = "Let's change you!" 
=> "Let's change you!" 
irb(main):005:0> post.save
=> true

Setting RAILS_ENV in your environment

Rails will check first to see if RAILS_ENV is set in the environment. For information about setting RAILS_ENV under \FastCGI see Switching to Production environment. (EDIT: This link contains no information about switching to a Production environment.)

WEBrick

Webrick starts in development mode by default. It accepts an environment setting on the command line via the -e switch (note that this overrides your RAILS_ENV setting).

$ script/server  -e production

Runner Script

Runner can be used to run a short script or just call a method within the Rails environment from the commandline:

$ script/runner -e production 'Import.run()'

Sample environment code.

You can use the following code in a helper file to create global variables that you can use in your application to test RAILS_ENV status. This code is fairly cautious to be sure that it only sets RAILS_ENV_PRODUCTION to true when it’s sure. But like any sample code, test it yourself before using it in an important project.


#setup global constants for manipulating states
RAILS_ENV_VAR = "RAILS_ENV" 
RAILS_ENV_DEV = ENV[RAILS_ENV_VAR]=="development" 
RAILS_ENV_TEST = ENV[RAILS_ENV_VAR]=="test" 
RAILS_ENV_PRODUCTION = !RAILS_ENV_DEV && !RAILS_ENV_TEST
# check positively: if production environment is implied because test and development are not found
# but production doesn't show up positively, raise exception
if RAILS_ENV_PRODUCTION && ENV[RAILS_ENV_VAR]!="production" 
  raise "Production environment implied but not detected." 
end

Rails has the concept of environments to represent the stages of an application’s lifecycle: test, development, and production are the defaults. Specify your choice with the RAILS_ENV environment variable.

RAILS_ENV can currently have one of three values:
  • development
  • test
  • production

Within a Rails application, RAILS_ENV can most easily be accessed with the following code:

ENV["RAILS_ENV"]

Each environment has a corresponding environment file (eg. config/environments/name.rb) with its particular config code.

Loading the rails environment gives you a fully configured context for accessing the model in an Rails application. This is extremely useful when you need to run upgrade or maintenance scripts. Or when you need to interrogate the model ad-hoc.

From the “console” script

There’s a helper script that will load the irb and your rails environment, with no additional configuration required.

$ script/console production

That’ll start up a IRB session for the “production” environment with all the nice settings in there (auto-completion and no inspection). You can read more about how to use the console here . This can be a super convenient place to create some database records.

From a Ruby script

Maintenance script example (placed in scripts/):

RAILS_ENV = 'production'
require File.dirname(__FILE__) + '/../config/environment'

for account in Account.find(:all)
  account.recalculate_interests
end

This will bring up the model and provide logging to log/production.log.

From IRB

You can manually load your environment and models into a running IRB session. It’s easier just to run script/console as described above, though, and get exactly the same thing.

$ irb
irb(main):001:0> require 'config/environment'
=> true
irb(main):002:0> require 'post'
=> true
irb(main):003:0> post = Post.find(1)
=> #<Post:0x124694c @attributes={"title"=>"su", "body"=>"asdf -- that's nice asd  asd", 
"author"=>"david", "id"=>"1", "written_on"=>"2004-07-25 00:00:00"}>
irb(main):004:0> post.title = "Let's change you!" 
=> "Let's change you!" 
irb(main):005:0> post.save
=> true

Setting RAILS_ENV in your environment

Rails will check first to see if RAILS_ENV is set in the environment. For information about setting RAILS_ENV under \FastCGI see Switching to Production environment. (EDIT: This link contains no information about switching to a Production environment.)

WEBrick

Webrick starts in development mode by default. It accepts an environment setting on the command line via the -e switch (note that this overrides your RAILS_ENV setting).

$ script/server  -e production

Runner Script

Runner can be used to run a short script or just call a method within the Rails environment from the commandline:

$ script/runner -e production 'Import.run()'

Sample environment code.

You can use the following code in a helper file to create global variables that you can use in your application to test RAILS_ENV status. This code is fairly cautious to be sure that it only sets RAILS_ENV_PRODUCTION to true when it’s sure. But like any sample code, test it yourself before using it in an important project.


#setup global constants for manipulating states
RAILS_ENV_VAR = "RAILS_ENV" 
RAILS_ENV_DEV = ENV[RAILS_ENV_VAR]=="development" 
RAILS_ENV_TEST = ENV[RAILS_ENV_VAR]=="test" 
RAILS_ENV_PRODUCTION = !RAILS_ENV_DEV && !RAILS_ENV_TEST
# check positively: if production environment is implied because test and development are not found
# but production doesn't show up positively, raise exception
if RAILS_ENV_PRODUCTION && ENV[RAILS_ENV_VAR]!="production" 
  raise "Production environment implied but not detected." 
end