Aron Solberg
Aron Solberg

Reputation: 6858

How does rails typically know what environment to run under?

i.e. when I'm running the app in test mode (using rails server) or maybe some other configuration it's running in develop mode (with no asset compilation, or caching, etc) but when I deploy it to a server its running in production mode.

How does the app determine what environment configuration to use?

Upvotes: 6

Views: 2389

Answers (1)

Marcelo De Polli
Marcelo De Polli

Reputation: 29281

Rails reads the current environment from the operating system's environment variables by checking the following in order of priority:

  1. Get the value of the RAILS_ENV environment variable by calling ENV["RAILS_ENV"]
  2. If the above is nil, then get ENV["RACK_ENV"]
  3. If the above is nil, then make it equal to "development"

You can see that behavior in the Rails source code by looking at the definition of the Rails.env method:

def env
  @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end

Source: https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails.rb#L55-L57

That's the method you call when you write Rails.env to find out the current environment.

Upvotes: 14

Related Questions