Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 486
asolberg Avatar asked Apr 01 '14 03:04

asolberg


People also ask

What environment does Rails have by default?

When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.

What are the environments in Rails?

Rails comes packages with 3 types of environments. Each have its own server, database, and configuration. See Rails Guides: Configuration for more information on options available to you.

What variable changes the environment in Ruby on Rails?

Gmail Example Instead use the Ruby variable ENV["GMAIL_USERNAME"] to obtain an environment variable. The variable can be used anywhere in a Rails application. Ruby will replace ENV["GMAIL_USERNAME"] with an environment variable. Let's consider how to set local environment variables.


1 Answers

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.

like image 57
Marcelo De Polli Avatar answered Nov 15 '22 10:11

Marcelo De Polli