In Rails 8.1, how do RAILS_ENV and the -e flag decide which environment bin/rails console, runner and server boot into?
answer
- RAILS_ENV, then RACK_ENV, then development
- -e written into RAILS_ENV before boot
- prefixes expand: -e prod
- Rake tasks take no -e
- Rake reads -e as --execute
basics
~20 sWithout a flag, bin/rails uses RAILS_ENV, then RACK_ENV, then development. The -e flag on server, console, runner and dbconsole overrides that for one command; Rake tasks such as db:migrate have no -e, so they need RAILS_ENV=production in front.
solid answer
~40 s`Rails::Command.environment` returns `RAILS_ENV` if it is set, else `RACK_ENV`, else `"development"`. Commands that include Rails' environment argument (`server`, `console`, `runner`, `dbconsole`, plus `boot`, `initializers` and `credentials`) accept `-e` / `--environment` as a per-command override; for console, runner and dbconsole Rails writes that value into `ENV["RAILS_ENV"]` just before it requires the application, so the flag wins over the shell. A short name expands when no `config/environments/<name>.rb` matches it: `-e prod` becomes `production`. Rake tasks are different: `bin/rails db:migrate -e production` is handed to Rake, which reads `-e` as its own `--execute` option, evaluates `production` as Ruby and stops before the task runs. For tasks, set the variable: `RAILS_ENV=production bin/rails db:migrate`.
code
bash · 7 linesbin/rails console # RAILS_ENV, then RACK_ENV, then development
bin/rails console -e prod # expands to production
RAILS_ENV=production bin/rails runner 'puts Rails.env'
# Rake tasks take no -e:
RAILS_ENV=production bin/rails db:migrate # works
bin/rails db:migrate -e production # Rake evals "production" and exitsgo deeper
Remember the default is development and that RAILS_ENV or -e changes it. Know that the console prompt shows which environment you are in.
Explain the lookup order RAILS_ENV, RACK_ENV, development, which commands take -e, and why Rake tasks need RAILS_ENV in front instead of -e.
Show the operational habits: explicit environments on production hosts, never trusting a bare db: task with -e, and checking Rails.env before running anything destructive.
Decide how environments are provisioned and named across the fleet, so that a missing RAILS_ENV fails loudly instead of silently booting development against real infrastructure.
## Where the environment name comes from A Rails **environment** is a name (`development`, `test`, `production`, or one you add such as `staging`) that selects `config/environments/<name>.rb`, the matching `database.yml` entry and the credentials file. Every `bin/rails` invocation settles on one before the application loads. With no flag, `Rails::Command.environment` decides, in this order: 1. `RAILS_ENV`, if present; 2. `RACK_ENV`, if present; 3. the literal `"development"`. That fallback is why a production container sets `RAILS_ENV=production` in its Dockerfile: anything that forgets it boots in development. ## The -e flag on commands Rails' own **commands** (Thor classes under `railties/lib/rails/commands`) that include the shared environment argument accept `-e` / `--environment`: | Command | Accepts `-e` | |---|---| | `server` | yes | | `console` | yes | | `runner` | yes | | `dbconsole` | yes | | `boot`, `initializers`, `credentials:*` | yes | | `db:migrate`, `tmp:clear` and other Rake tasks | no | For `console`, `runner` and `dbconsole`, the environment argument assigns `ENV["RAILS_ENV"] = <value>` immediately before requiring `config/application.rb`. The flag therefore **beats** whatever the shell exported, and everything downstream (`Rails.env`, the database config, `Rails.env.production?`) agrees with it. Two conveniences come with it: - **Prefix expansion.** If the value is not the name of a file in `config/environments/`, Rails tries to expand it against `production`, `development` and `test`: `-e prod` boots production, `-e dev` development. An app that really has `config/environments/prod.rb` keeps `prod` as is. - **Visible confirmation.** The console prints `Loading production environment (Rails 8.1.4)` and tags the prompt `prod` in red, so you know where you are before you type. ## Why Rake tasks are the trap `bin/rails` first looks for a command class matching the name. When none exists, as for `db:migrate`, it hands the whole argument list to **Rake**. Rake parses its own options, and in Rake `-e` means `--execute CODE`: evaluate the code and exit. So: - `bin/rails db:migrate -e production` asks Rake to `eval("production")`, which raises a `NameError`, and the migration never runs; - `RAILS_ENV=production bin/rails db:migrate` is the working form, because the task's `:environment` prerequisite boots the app with whatever `RAILS_ENV` says. The same applies to every Rake task, including your own in `lib/tasks`. ## Practical rules - In **development**, rely on the default and type nothing. - For a **one-off production console or runner**, `-e production` is explicit and survives a shell where `RAILS_ENV` is unset; on a production host it is usually set already. - For **tasks**, always prefix `RAILS_ENV=...`. - Use `Rails.env.local?` (true for development and test) in code that must behave differently on your laptop, rather than comparing strings. - A **custom environment** needs its own `config/environments/staging.rb`, a `database.yml` entry and usually its own credentials; `-e staging` alone does not create any of them. Getting this wrong is how people run a destructive script against the wrong database, or believe they migrated production when Rake quietly evaluated a word and exited. ## How the running app sees the choice Once booted, the chosen name is available as `Rails.env`, an `ActiveSupport::EnvironmentInquirer`. It answers predicate methods for any name (`Rails.env.production?`, `Rails.env.staging?`) and the dedicated `Rails.env.local?`. Three things are picked by that name during boot: | Picked by the environment | Example for production | |---|---| | the environment file | `config/environments/production.rb` | | the database entry | the `production:` block of `config/database.yml` | | per-environment credentials, when present | `config/credentials/production.yml.enc` | If the name is wrong, all three are wrong together, which is why a mistyped or missing environment produces confusing errors (an unknown database, missing credentials) rather than an obvious one. ## A worked comparison On a host where the shell exported `RAILS_ENV=staging`: - `bin/rails console` boots **staging**; - `bin/rails console -e production` boots **production**, because the flag is written into `RAILS_ENV` before the app loads; - `bin/rails runner -e production 'puts Rails.env'` prints `production`; - `bin/rails db:migrate` migrates **staging**, and adding `-e production` breaks the run instead of switching it.
- Why does bin/rails db:migrate -e production fail instead of migrating production?No Rails command class is named `db:migrate`, so `bin/rails` passes the arguments to Rake. Rake treats `-e` as `--execute`, evaluates `production` as Ruby, which raises a `NameError`, and exits before any task runs. Rake tasks pick their environment from `RAILS_ENV` when the `:environment` prerequisite boots the app, so write `RAILS_ENV=production bin/rails db:migrate`.
- What does Rails.env.local? return, and why prefer it to comparing Rails.env with a string?`Rails.env.local?` is true in development and test and false elsewhere. It names the intent (my machine or CI, not a deployed server) and keeps working when a team adds a custom deployed environment such as staging, where a check like `Rails.env != "production"` would wrongly treat staging as local.
- bin/rails db:migrate -e production migrates the production database
- RACK_ENV is ignored entirely by bin/rails commands
- With RAILS_ENV unset, bin/rails console on a server boots production
- The -e flag only accepts full environment names like production
- Exported RAILS_ENV always beats the -e flag on bin/rails console