skip to content

In a Rails app, what does config.load_defaults 8.1 do, and why does the app keep 8.0 behaviour right after bundle update rails?

level: middleimportance: must knowfreq 58%

answer

  1. the gem and the defaults move separately
  2. cumulative: 8.1 loads 8.0 first
  3. app:update keeps your current number
  4. new_framework_defaults_8_1.rb, one line at a time
  5. YJIT flips with no preview line

basics

~10 s

config.load_defaults applies every framework default introduced up to that version. Upgrading the gem leaves the line at 8.0, so 8.0 defaults stay until you flip the lines in new_framework_defaults_8_1.rb and raise load_defaults to 8.1.

solid answer

~40 s

`config.load_defaults` in `config/application.rb` sets the framework defaults for a target version and every version before it: `load_defaults "8.1"` first calls `load_defaults "8.0"`, then applies the 8.1 changes; an unknown number raises `Unknown version`. The installed gem and this number move separately. `bundle update rails` changes the code, and `bin/rails app:update` regenerates `config/application.rb` with the app's current `load_defaults` value, so behaviour that hangs on new defaults stays at 8.0. Instead the task writes `config/initializers/new_framework_defaults_8_1.rb`, which lists each 8.1 default commented out. You uncomment them one at a time, deploy and watch, and when all are on you delete the file and set `config.load_defaults 8.1`. One 8.1 default has no line there: `yjit` becomes `!Rails.env.local?`, so YJIT turns off in development and test only when the number changes.

code

ruby · 12 lines
ruby
# config/application.rb
module Catalogue
  class Application < Rails::Application
    # Still 8.0 after bundle update rails and bin/rails app:update.
    config.load_defaults 8.0
  end
end

# config/initializers/new_framework_defaults_8_1.rb
# Uncommented one at a time, one deploy each:
Rails.configuration.action_controller.action_on_path_relative_redirect = :raise
# Rails.configuration.action_view.render_tracker = :ruby

go deeper

for a junior

Know that config.load_defaults in config/application.rb names a defaults version separate from the installed Rails gem.

for a middle

Explain the cumulative chain, the new_framework_defaults file as a staging area, and why app:update keeps your number.

for a senior

Plan the flips one per deploy, know which defaults have no preview line, and keep deliberate exceptions after the load_defaults call.

for a principal

Set a rule for how far the defaults version may lag the gem across apps, so upgrades never accumulate several defaults sets.

## Two version numbers in every Rails app A Rails app carries two version numbers that move independently: - the **framework version**, the `rails` gem in `Gemfile.lock` (8.1.4 at this pin); - the **defaults version**, the argument to `config.load_defaults` in `config/application.rb`. New Rails releases often change a default: a header value, a serializer, how a redirect is checked. Changing those silently on a gem upgrade would break running apps, so Rails ties them to the second number instead. A new 8.1 app is generated with `config.load_defaults 8.1`; an app upgraded from 8.0 keeps saying `8.0` until someone changes it. ## How load_defaults works `load_defaults` lives in `railties/lib/rails/application/configuration.rb` as one `case` over version strings: 1. Each branch first calls the previous version, so `load_defaults "8.1"` runs the `"8.0"` branch, which runs `"7.2"`, and so on down the chain. The defaults are **cumulative**. 2. Each branch then sets that version's new values, guarded by `respond_to?(:active_record)` and similar checks, so a framework the app does not load is skipped. 3. Any number Rails does not know raises `Unknown version`. 4. The value is remembered as `config.loaded_config_version`. Because the method assigns values, a setting written **before** `config.load_defaults` in `config/application.rb` is overwritten whenever that version's defaults set the same key; overrides belong after the call, in environment files or in initializers. ## What 8.1 changes The `"8.1"` branch sets the following. All but the first also appear, commented out, in `new_framework_defaults_8_1.rb`: | Setting | 8.1 value | Effect | |---|---|---| | `yjit` | `!Rails.env.local?` | YJIT off in development and test (no line in the defaults file) | | `action_controller.escape_json_responses` | `false` | JSON renderer stops escaping HTML entities | | `action_controller.action_on_path_relative_redirect` | `:raise` | `redirect_to "example.com"` raises `ActionController::Redirecting::UnsafeRedirectError` instead of logging | | `active_record.raise_on_missing_required_finder_order_columns` | `true` | `first` and friends raise `ActiveRecord::MissingRequiredOrderError` when no order or order column exists | | `active_support.escape_js_separators_in_json` | `false` | U+2028 and U+2029 are no longer escaped in JSON | | `action_view.render_tracker` | `:ruby` | template dependencies tracked with a Ruby parser | | `action_view.remove_hidden_field_autocomplete` | `true` | hidden fields from `form_tag`, `token_tag` and `button_to` drop `autocomplete="off"` | The defaults file notes that the finder-order option "will be removed in Rails 8.2", which is a plan stated in the file, not something 8.1 has done. ## The upgrade workflow `bin/rails app:update` keeps your existing `load_defaults` number when it rewrites `config/application.rb` and creates `config/initializers/new_framework_defaults_8_1.rb`. The file is an initializer, so its lines run after `config/application.rb` and override what `load_defaults 8.0` set. That makes it a staging area: 1. Uncomment one line, for example `Rails.configuration.action_controller.action_on_path_relative_redirect = :raise`. 2. Run the tests, deploy, and watch logs and errors. 3. Repeat for the next line. Several deploys are fine; the upgrade guide expects this to happen gradually. 4. When every line is on, **delete the file** and change the application line to `config.load_defaults 8.1`. 5. Watch for the defaults that have no line, such as YJIT in development and test. Any line you deliberately keep at the old value moves to `config/application.rb` after the `load_defaults` call, so the decision is visible. ## Checking what an app actually runs When behaviour differs between two apps on the same Rails version, compare their defaults, not their gems: - `Rails.application.config.loaded_config_version` in a console returns the number `load_defaults` was called with. - A single setting can be read directly, for example `Rails.application.config.action_controller.action_on_path_relative_redirect`, which is `:raise` under 8.1 defaults. - The configuring guide lists, for each setting whose default moved, the value per `load_defaults` version ("The default value depends on the `config.load_defaults` target version"). - `config/initializers/new_framework_defaults_*.rb` files that still exist show an upgrade that was started and not finished. These checks matter most in a portfolio of apps, where "we are on 8.1" can hide one app still running 7.1 behaviour. ## Traps - **Raising the number in the same commit as the gem.** It flips every default at once and hides which one broke a page. - **Assuming the gem version decides behaviour.** An app on Rails 8.1 with `load_defaults 7.1` runs 7.1 defaults for everything 7.2 to 8.1 changed. - **Leaving the defaults file forever.** After the number is raised, its uncommented lines are redundant, and forgotten commented ones mislead the next reader.

  • Why does a setting placed above config.load_defaults in config/application.rb seem to be ignored?
    `load_defaults` assigns the defaults for every version up to its target when it runs. A setting written before that call is overwritten if the version's defaults include it. Put overrides after the call, in `config/environments/*.rb`, or in an initializer.
  • Which 8.1 default changes when you raise load_defaults to 8.1 but cannot be previewed from new_framework_defaults_8_1.rb?
    `config.yjit`. The 8.1 branch sets it to `!Rails.env.local?`, turning YJIT off in development and test, but the defaults file has no line for it. Expect that change at the moment the number moves, not earlier.
  • What happens if you write config.load_defaults 8.2 on Rails 8.1?
    Boot fails with `Unknown version "8.2"`, because the `case` in `load_defaults` only knows the versions up to the installed release. The number can never be ahead of the gem.

  • Upgrading the rails gem switches the app to the new framework defaults automatically.
  • load_defaults 8.1 applies only the defaults introduced in 8.1.
  • bin/rails app:update raises config.load_defaults to the new version.
  • Every 8.1 default can be previewed from new_framework_defaults_8_1.rb.
  • Flip load_defaults in the same deploy as the gem bump to save time.

Assumes Rails 8.1.4; the values listed are the "8.1" branch of load_defaults and the new_framework_defaults_8_1.rb template at that pin.