In a Rails 8.1 museum-ticketing app, config/initializers/booking_limits.rb reads a flag set in feature_flags.rb and sees nil; why, and how would you fix it?
answer
- config/initializers load sorted by path
- b sorts before f
- config.x returns nil, not an error
- define flags in application.rb
- bang readers raise on false
basics
~20 sFiles in config/initializers load in sorted path order, so booking_limits.rb runs before feature_flags.rb, and config.x returns nil for a key nobody has set yet. Set the flag in application.rb or the environment file, or read it later.
solid answer
~40 sRails loads every `.rb` file under `config/initializers`, including subfolders, **sorted by path**, once each. `booking_limits.rb` sorts before `feature_flags.rb`, so when it reads `Rails.configuration.x.flags.timed_entry` the flag is unset. `config.x` does not complain: `x.flags` creates an empty `ActiveSupport::OrderedOptions` on demand and an unset key reads `nil`, which looks like "off". The robust fix is to set the flag in a phase that runs before every initializer: `config/application.rb` or `config/environments/<env>.rb`, or load a YAML file there with `config_for`. Failing that, read the flag lazily: at call time, or in a `to_prepare` or `after_initialize` block, which run after every initializer. A numeric prefix such as `00_feature_flags.rb` works but hides the dependency in a file name. Bang readers like `timed_entry!` raise `KeyError` on a blank value, which suits required strings but not booleans, because `false` is blank too.
code
ruby · 7 lines# config/initializers/booking_limits.rb (runs before feature_flags.rb)
BOOKING_LIMIT =
Rails.configuration.x.flags.timed_entry ? 40 : 120 # nil here, so 120
# config/initializers/feature_flags.rb
Rails.application.config.x.flags.timed_entry =
ENV.fetch("TIMED_ENTRY", "off") == "on"go deeper
Recall that the files in config/initializers run one after another in alphabetical order, so one file can run before another that it depends on.
Explain the sorted-path load, why config.x answers nil for an unset key, and why a value read at load time is frozen at that moment.
Fix it structurally: move shared values into application.rb or the environment file, read dependent values lazily or in to_prepare or after_initialize, and make required settings fail loudly without tripping on false.
Set a convention for where flags and settings are defined and read, so initializers never depend on each other's file names.
## What happened The museum-ticketing app has two initializers: - `config/initializers/feature_flags.rb` sets `Rails.application.config.x.flags.timed_entry` from an environment variable. - `config/initializers/booking_limits.rb` computes the maximum tickets per slot, using a smaller limit when timed entry is on. In every environment the limit comes out as if timed entry were off. Nothing raises. ## Why: the load order of config/initializers The application's `load_config_initializers` step collects every file matching `**/*.rb` under `config/initializers`, **sorts the full paths** and `load`s each one once. There is no dependency declaration between these files; the file name is the order. - `booking_limits.rb` sorts before `feature_flags.rb`, so it runs first. - Files in subfolders take their place by folder name: `config/initializers/admin/audit.rb` runs before `booking_limits.rb`. - All of them run after the frameworks' own initializers and before the finisher phase. ## Why nil instead of an error `config.x` is built for this kind of namespaced value, and it is forgiving: - `config.x.flags` returns the existing namespace or creates an empty `ActiveSupport::OrderedOptions` on first access. - `.timed_entry` on that namespace returns `nil` when the key was never assigned. So the early read silently yields `nil`, which Ruby treats as false. The bug looks like a wrong flag value, not a load-order problem. ## Fixes, from most to least robust 1. **Set the flag in an earlier phase.** The `config/application.rb` class body and `config/environments/<env>.rb` both run before any file in `config/initializers`. Put `config.x.flags.timed_entry = ...` there, or load a YAML file there with `config.x.flags = config_for(:feature_flags)`. 2. **Read lazily.** Do not capture the value when the initializer file loads. Read `Rails.configuration.x.flags.timed_entry` when the limit is needed, or move the setup into a `to_prepare` block (when it configures a reloadable class from `app/`) or an `after_initialize` block (for one-time setup); both run after every initializer. 3. **Rename to force the order.** `00_feature_flags.rb` sorts first and works, but the dependency is now encoded in a file name that the next person may rename. ## Making the failure loud `ActiveSupport::OrderedOptions` supports bang readers: `config.x.flags.timed_entry!` returns the value or raises `KeyError` (`:timed_entry is blank`) when it is blank. That catches an unset value at boot, but `false` is blank as well, so a flag that is legitimately off would also raise. Use bang readers for required, non-boolean values such as a pricing service URL, and an explicit `nil?` check for booleans. ## Why the failure is easy to miss - The early read does not raise, so boot succeeds and tests that never turn the flag on pass. - A developer who checks the flag in `bin/rails console` sees the right value, because the console runs after `initialize!` has finished, while the constant computed at boot still holds the wrong one. - Renaming either file, say to `limits/booking.rb`, silently changes the order again. ## Preventing a repeat 1. Agree that `config/initializers` files configure libraries and objects, and never read values that another initializer writes. 2. Keep app-wide values (`config.x`, `config_for`) in `application.rb` or the environment files, so they exist before any initializer runs. 3. Add a test that boots with the flag on and asserts the dependent value, such as the slot limit, so an ordering regression fails in CI. ## Debugging the order - `bin/rails initializers` lists initializer names in run order; the files in `config/initializers` appear inside the single `load_config_initializers` step, so their order is simply the sorted file list. - `ls config/initializers` with names compared as strings shows the same order. - A temporary `Rails.logger.info` line at the top of each suspect file confirms it in the boot log. | Approach | Survives a rename | Works for every initializer | |---|---|---| | Set in `application.rb` or the environment file | yes | yes | | Read at call time or in `after_initialize` | yes | yes | | Numeric file prefix | no | yes |
- In Rails 8.1, in what order does load_config_initializers run config/initializers/admin/audit.rb, booking_limits.rb and feature_flags.rb?`admin/audit.rb`, then `booking_limits.rb`, then `feature_flags.rb`. Rails globs `**/*.rb` under `config/initializers` and sorts the full paths as strings, so a subfolder's files sit where the folder name sorts, here before `booking_limits.rb`.
- In the Rails 8.1 museum-ticketing app, why does bin/rails console show the flag as on while bookings behave as if it were off?The console starts after `initialize!` has finished, so every initializer, including `feature_flags.rb`, has run and the flag reads correctly. The booking limit was computed earlier, while `booking_limits.rb` loaded, and stored in a constant; that value never changes after boot, so the console cannot reveal the ordering bug.
- Rails loads config/initializers in the order the files were created.
- Reading an unset config.x key raises NoMethodError, so the bug would surface at boot.
- A bang reader such as timed_entry! is safe for boolean flags.
- Files in subfolders of config/initializers are never loaded.
- A numeric file prefix is the cleanest way to declare dependencies between initializers.