skip to content

In Rails 8.1, what changes when the pet-insurance quote app's staging runs RAILS_ENV=staging with its own environment file instead of RAILS_ENV=production with staging ENV values?

level: seniorimportance: should knowfreq 30%

answer

  1. staging.rb, often require_relative production
  2. database.yml needs a staging key
  3. Gemfile :production group not required
  4. production? branches skip staging
  5. same code path, different ENV

basics

~20 s

RAILS_ENV=staging needs its own staging.rb, a database.yml entry and staging credentials, and skips production?-only code and :production gems. RAILS_ENV=production with staging ENV values runs the exact production code path and differs only in configuration values.

solid answer

~40 s

With `RAILS_ENV=staging`, Rails loads `config/environments/staging.rb` if it exists; a common file is `require_relative "production"` plus overrides. You also need a `staging:` key in `database.yml` (or a `DATABASE_URL` for the primary database), or Active Record raises `ActiveRecord::AdapterNotSpecified`, and either `SECRET_KEY_BASE` or staging credentials, since staging is not `local?`. `Bundler.require(*Rails.groups)` loads the `:staging` group, not `:production`, and every `Rails.env.production?` branch is skipped. With `RAILS_ENV=production` on staging servers, staging runs exactly the production configuration, gems and branches, and differs only through values such as `DATABASE_URL`, `CDN_HOST` or `RAILS_LOG_LEVEL`; the cost is that the app cannot tell itself apart, so anything staging-specific needs an explicit setting. A missing `staging.rb` is the classic failure: Rails boots with only `application.rb` and warns that `config.eager_load` is nil.

go deeper

for a junior

Know that RAILS_ENV=staging needs its own environment file and database entry, and that production? is false there.

for a middle

Explain how Bundler groups, credentials and Rails.env checks change with a named staging environment, and what a missing staging.rb does.

for a senior

Argue for one design given what staging must prove, and remove production?-only branches so staging exercises the real code path.

for a principal

Set the policy for pre-production environments, how close to production they must be, and who owns drift between their configurations.

## The two designs Teams run staging in one of two ways: 1. **A named environment**: `RAILS_ENV=staging`, with `config/environments/staging.rb`, a `staging:` section in `config/database.yml` and its own credentials. 2. **Production settings, staging values**: `RAILS_ENV=production` on staging servers, with every difference expressed as an environment variable the production configuration already reads. Both work. The difference is how much code runs differently between staging and production. ## What a named `staging` environment needs - **An environment file.** Rails' `load_environment_config` initializer requires `config/environments/#{Rails.env}.rb` only if the file exists. A minimal one reuses production: ```ruby # config/environments/staging.rb require_relative "production" Rails.application.configure do config.log_level = :debug end ``` - **A database entry.** Without a `staging:` key in `database.yml`, and with no `DATABASE_URL` to build the primary configuration from, Active Record raises `ActiveRecord::AdapterNotSpecified` ("The `staging` database is not configured for the `staging` environment"). - **A secret.** `Rails.env.local?` is false for staging, so Rails will not generate a `secret_key_base`; provide `SECRET_KEY_BASE` or staging credentials (`bin/rails credentials:edit --environment staging`). - **The right gems.** `config/application.rb` calls `Bundler.require(*Rails.groups)`, which requires the default group and the group named after the environment. Gems you placed in `group :production` are installed but **not required** on staging. ## What silently differs with a named environment | Concern | `RAILS_ENV=staging` | `RAILS_ENV=production` with staging values | |---|---|---| | Environment file | `staging.rb` (or nothing, if missing) | `production.rb`, identical to production | | `Rails.env.production?` branches | Skipped | Run exactly as in production | | `group :production` gems | Not required | Required | | Credentials | `config/credentials/staging.yml.enc` | Production's file or key, unless overridden by ENV | | Telling staging apart in code | `Rails.env.staging?` | Needs a variable of your own | The middle rows are where staging stops testing production: an error reporter initialised inside `if Rails.env.production?`, an SSL setting, a cache store or a gem in the production group all behave differently, so a staging pass proves less. ## The classic failure: a missing `staging.rb` If `RAILS_ENV=staging` is set but no `staging.rb` exists, nothing raises at that step. Rails boots with `config/application.rb` alone, prints the warning "config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly", and derives eager loading from the reloading setting. None of production's settings apply: no `silence_healthcheck_path`, no far-future `public_file_server` headers, no production logger setup. The app may appear to work while running with development-like defaults. ## Production settings with staging values The second design keeps one environment file and moves every difference into configuration values that the generated files already read, or that you add: - `RAILS_LOG_LEVEL` (the generated `config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")`); - `DATABASE_URL` or the generated `DB_HOST`; - `CDN_HOST` for `config.asset_host`; - the mailer host and other URLs. Its costs: staging uses production's credentials file unless you supply secrets through ENV, every staging-only behaviour must be an explicit flag of your own, and logs and error reports must carry the deploy's name from configuration, since `Rails.env` reads `production` everywhere. ## Rolling out a named staging environment If the team chooses `RAILS_ENV=staging`, the minimum change set is: 1. add `config/environments/staging.rb`, usually starting with `require_relative "production"`; 2. add `staging:` entries to `config/database.yml` for every database the app uses (a `DATABASE_URL` covers only the primary one); 3. create staging credentials, or provide `SECRET_KEY_BASE` and other secrets through the environment; 4. review the Gemfile for `group :production` gems and every `Rails.env.production?` check; 5. set `RAILS_ENV=staging` (and `RACK_ENV`, if anything sets it) in the staging deploy. ## Choosing for the quote app - Prefer **production plus ENV** when staging exists to rehearse deploys and the health-checked rollout: the code path is identical, so the load balancer, `/up` and asset setup are tested for real. - Prefer **a named environment** when staging must differ structurally, for example a sandbox payment gateway selected in configuration, and you accept maintaining `staging.rb`, its database entry and credentials. - Either way, avoid `Rails.env.production?` in application code; read a configuration value instead, so both designs behave the same.

  • The quote app adds `gem "quote_audit", group: :production` and staging runs with `RAILS_ENV=staging`. What happens?
    The gem is installed, since `BUNDLE_WITHOUT` excludes only development, but `Bundler.require(*Rails.groups)` requires `:default` and `:staging`, not `:production`. On staging the gem's code is never loaded, so anything that references it fails, or the audit silently never runs. Put it in the default group, or add `:staging` to its groups.
  • Why is `require_relative "production"` at the top of `staging.rb` a common pattern, and what is its catch?
    It runs production's `Rails.application.configure` block first, so staging inherits every production setting and lists only its overrides. The catch is that every production change now reaches staging too, including settings that need staging-specific values, so review `staging.rb` whenever `production.rb` changes.

  • A missing staging.rb makes Rails fall back to production.rb
  • Gems in group :production also load when RAILS_ENV=staging
  • Rails.env.local? is true on staging, so a secret is generated
  • A staging deploy reuses the production entry in database.yml
  • Running RAILS_ENV=production on staging lets Rails.env.staging? still work