How does Rails 8.1 decide `Rails.env` from `RAILS_ENV` and `RACK_ENV`, and what do `Rails.env.production?` and `Rails.env.local?` return on a staging deploy?
answer
- RAILS_ENV, then RACK_ENV, then development
- .presence ignores empty strings
- EnvironmentInquirer answers staging?
- local? means development or test
- "local" is a reserved name
basics
~10 sRails.env takes RAILS_ENV, falls back to RACK_ENV, then to "development", ignoring empty values. With RAILS_ENV=staging, Rails.env.staging? is true while production? and local? are false; local? is true only for development and test.
solid answer
~40 s`Rails.env` is memoized as `ActiveSupport::EnvironmentInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")`, and `bin/rails` commands resolve their default the same way; `-e staging` writes `RAILS_ENV`. `RACK_ENV` is the Rack-wide convention other tools read, while `RAILS_ENV` is Rails' own and wins when both are set. Because of `.presence`, `RAILS_ENV=""` counts as unset. The inquirer answers `production?`, `development?` and `test?` directly and any other `name?` through `StringInquirer`, so on staging `Rails.env.staging?` is true and `Rails.env.production?` is false. `Rails.env.local?` is true only for development and test, so staging gets no auto-generated `secret_key_base` and none of the dev-only branches. Naming an environment `local` raises `ArgumentError`. Puma's own CLI reads `APP_ENV`, then `RACK_ENV`, then `RAILS_ENV`, so set the variables consistently.
go deeper
Remember that RAILS_ENV picks the environment, that it defaults to development, and that the Docker image sets production.
Explain the RAILS_ENV, RACK_ENV, development fallback with .presence, how the inquirer answers any name?, and what local? covers.
Spot environment mismatches between platform and app, avoid production?-only branches staging should share, and know what local? switches off on deployed environments.
Define how many named environments the organisation runs and which differences belong in environment files versus configuration values.
## How Rails picks its environment `Rails.env` is defined in `railties/lib/rails.rb`: ```ruby def env @_env ||= ActiveSupport::EnvironmentInquirer.new( ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development" ) end ``` Reading it in order: 1. **`RAILS_ENV`** is Rails' own variable and wins whenever it is non-blank. 2. **`RACK_ENV`** is the general Rack convention; Rails uses it only as a fallback. 3. With neither set, the environment is **`development`**. `.presence` turns an empty string into `nil`, so `RAILS_ENV=""` behaves as if it were unset. The value is memoized in `@_env`; changing `ENV` after boot does not change `Rails.env`, although code can assign `Rails.env = "staging"` explicitly. `bin/rails` commands use the same order to choose their default (`Rails::Command.environment`), and the `-e` / `--environment` option writes `RAILS_ENV` before the app loads. `bin/rails server` sets `RAILS_ENV` only if it is not already set. The generated Dockerfile sets `RAILS_ENV="production"` in its base stage, so every process in the image runs in production unless the deploy overrides it. ## What the inquirer answers `EnvironmentInquirer` is a `StringInquirer` with fast paths: | Call | `RAILS_ENV=production` | `RAILS_ENV=staging` | `RAILS_ENV=test` | |---|---|---|---| | `Rails.env` | `"production"` | `"staging"` | `"test"` | | `Rails.env.production?` | `true` | `false` | `false` | | `Rails.env.staging?` | `false` | `true` | `false` | | `Rails.env.local?` | `false` | `false` | `true` | - `production?`, `development?` and `test?` are defined methods; any other `name?` goes through `StringInquirer`'s `method_missing`, comparing the name with the string. - **`local?`** is true for `development` and `test` only. A custom environment is never local. - Creating an inquirer for `"local"` raises **`ArgumentError`** ("'local' is a reserved environment name"), so you cannot name an environment `local`. ## Why `local?` matters on staging Rails uses `Rails.env.local?` for behaviour that is safe only on a developer machine. The clearest example is `secret_key_base`: when neither `SECRET_KEY_BASE` nor credentials provide one, Rails generates and stores a random secret in `tmp/local_secret.txt` **only** in local environments. A staging deploy, like production, raises `ArgumentError` for a missing secret instead. Checks of your own that read `Rails.env.local?` behave the same way: staging is treated as a deployed environment. ## `RACK_ENV` and the server `RACK_ENV` exists because Rack tools need an environment name without knowing which framework they serve. Puma's command-line launcher reads `APP_ENV`, then `RACK_ENV`, then `RAILS_ENV`, the reverse of Rails' priority, and then sets `RACK_ENV` to what it chose. It uses that name, for example, to look for `config/puma/<environment>.rb` before `config/puma.rb`. If a platform sets `RACK_ENV=production` while you set `RAILS_ENV=staging`: - Rails runs as `staging`, because `RAILS_ENV` wins inside Rails; - the server's own environment-specific choices may still follow `production`. The safe rule: set `RAILS_ENV` for Rails, and keep `RACK_ENV` either unset or equal to it. ## Setting it in practice - In the generated Docker image `RAILS_ENV` is already `production`; a staging deploy that wants a named environment must override it, for example through the deploy tool's environment settings. - On a developer machine nothing is set, so everything runs in `development`, and `bin/rails test` switches to `test` itself. - For a one-off command against another environment, `bin/rails console -e staging` is clearer than exporting a variable that later commands inherit. ## Pitfalls to name in an interview - Branching on `Rails.env.production?` for behaviour staging should share, such as error reporting or SSL settings; prefer configuration values or `!Rails.env.local?`. - Expecting `RACK_ENV` to override a `RAILS_ENV` baked into the image. - Assuming an unknown environment falls back to production settings; Rails loads `config/environments/<name>.rb` only if it exists. - Changing `ENV["RAILS_ENV"]` in an initializer and expecting `Rails.env` to follow.
- The pet-insurance staging container has `RACK_ENV=production` from the platform and `RAILS_ENV=staging` from the deploy. Which environment does the app run in?Rails reads `RAILS_ENV` first, so `Rails.env` is `"staging"` and `config/environments/staging.rb` is loaded. The mismatch can still mislead tools that prefer `RACK_ENV`, such as Puma's launcher when it picks an environment-specific config file, so set both to `staging` or drop `RACK_ENV`.
- Why does Rails reserve the environment name `local`?`Rails.env.local?` is a predicate meaning development or test. An environment literally named `local` would collide with that meaning, so `EnvironmentInquirer` refuses the name and raises `ArgumentError` ("'local' is a reserved environment name").
- RACK_ENV overrides RAILS_ENV when both are set
- Rails.env.local? is true on staging because it is not production
- Rails.env.staging? raises NoMethodError without a custom predicate
- An empty RAILS_ENV makes Rails run in an environment named ""
- Rails re-reads RAILS_ENV on every request