skip to content

In a Rails 8.1 app's config/application.rb, what does `require "rails/all"` load, and how is a framework such as Action Cable left out?

level: juniorimportance: should knowfreq 30%

answer

  1. one railtie or engine per framework
  2. action_cable/engine, active_record/railtie
  3. skipped lines are commented out
  4. config.action_cable comes from the railtie
  5. LoadError is rescued in rails/all

basics

~10 s

require "rails/all" requires each framework's railtie or engine, such as active_record/railtie and action_cable/engine, which registers its initializers and config namespace. An app without Action Cable requires frameworks one by one, leaving "action_cable/engine" commented out.

solid answer

~30 s

Every Rails framework plugs into the app through a `Rails::Railtie` or a `Rails::Engine`. In Rails 8.1, `rails/all` requires `active_record/railtie`, `active_storage/engine`, `action_controller/railtie`, `action_view/railtie`, `action_mailer/railtie`, `active_job/railtie`, `action_cable/engine`, `action_mailbox/engine`, `action_text/engine` and `rails/test_unit/railtie`, rescuing `LoadError` for each. Requiring one defines its initializers and its config namespace, such as `config.action_cable`. When `rails new` skips a framework, `config/application.rb` lists the requires one by one and comments out the skipped line, for example `# require "action_cable/engine"`. Removing a framework later means commenting its require and deleting its `config.action_cable` lines, because without the railtie that namespace no longer exists and the setting raises `NoMethodError` at boot.

code

ruby · 16 lines
ruby
# config/application.rb of an app generated with --skip-action-cable
require_relative "boot"

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "rails/test_unit/railtie"

go deeper

for a junior

Know that rails/all requires one railtie or engine per framework and that skipping writes a commented require line.

for a middle

Explain what a framework railtie contributes: a config namespace, initializers and tasks. Say why config.action_cable vanishes with its require.

for a senior

Trim frameworks deliberately in API or worker apps, clean the matching config, and verify the result with bin/rails initializers.

for a principal

Prefer an explicit framework list in long-lived apps so upgrades never add a framework silently.

## Frameworks are railties Rails has no built-in knowledge of Active Record, Action Mailer or Action Cable. Each framework ships a class that subclasses `Rails::Railtie` (or `Rails::Engine`, which is a railtie with an `app/` tree, routes and migrations) and that class tells Rails how to initialize it. The `Rails::Railtie` documentation states the design directly: every major component implements a railtie and is responsible for its own initialization, so Rails itself has no component hooks. What a framework's railtie typically contributes: - a **config namespace**, for example `config.action_mailer = ActiveSupport::OrderedOptions.new` in `ActionMailer::Railtie` and `config.active_record = ActiveSupport::OrderedOptions.new` in `ActiveRecord::Railtie`; - **initializers** that copy those settings onto the framework classes, usually inside `ActiveSupport.on_load` blocks so the classes are not loaded early; - **rake tasks**, console hooks and generators where relevant (the `db:` tasks come from Active Record's railtie). ## What `rails/all` requires In Rails 8.1 `railties/lib/rails/all.rb` requires `rails` and then, each inside a `begin/rescue LoadError`: | Require | Kind | Why an engine | |---|---|---| | `active_record/railtie` | railtie | | | `active_storage/engine` | engine | ships models, controllers, routes and migrations | | `action_controller/railtie` | railtie | | | `action_view/railtie` | railtie | | | `action_mailer/railtie` | railtie | | | `active_job/railtie` | railtie | | | `action_cable/engine` | engine | ships JavaScript under `app/` and mounts its server route in the host | | `action_mailbox/engine` | engine | ships models, controllers, routes and migrations | | `action_text/engine` | engine | ships models, helpers, views and migrations | | `rails/test_unit/railtie` | railtie | | Active Model's railtie is not in the list because Active Record's railtie requires `active_model/railtie` itself. ## What a skipping app writes instead `rails new` writes `require "rails/all"` only when no framework is skipped. Otherwise the generator writes one line per framework and comments out the skipped ones: ```ruby require "rails" # Pick the frameworks you want: require "active_model/railtie" require "active_job/railtie" require "active_record/railtie" # require "action_cable/engine" ``` That list also includes `active_model/railtie`, which `rails/all` reaches indirectly. The generator also leaves out the matching settings: with `--skip-action-mailer` the environment files carry no `config.action_mailer` lines. ## Removing a framework later 1. Replace `require "rails/all"` with the explicit list and comment out the framework, for example `# require "action_cable/engine"`. 2. Delete every `config.action_cable...` line in `config/application.rb`, `config/environments/*.rb` and initializers. The `config` object answers unknown names through `method_missing` only for namespaces a railtie defined; without the railtie, `config.action_cable` raises `NoMethodError` while the app boots. 3. Remove files that reference the framework's classes (`app/channels`, `config/cable.yml`), and the gem if it is listed separately (the Solid Cable gem, for example). 4. Boot the app and run `bin/rails initializers` to confirm the framework's initializers are gone. ## Gems join the same way The next line of `config/application.rb`, `Bundler.require(*Rails.groups)`, requires every gem in the Gemfile. A gem that defines a railtie subclass is registered the moment the class is defined, so its initializers join the same boot sequence as the frameworks' and its config namespace becomes available in the `Application` class body that follows. That ordering, frameworks first and gems next, is why `config/application.rb` can configure both. ## Checking what loaded A console session answers most questions about which railties a process has: - `Rails::Railtie.subclasses` lists the plain railties that were defined, in load order, frameworks first; - `Rails::Engine.subclasses` lists the engines, including `ActiveStorage::Engine` and any gem engines; - `Rails.application.config.respond_to?(:action_cable)` returns `false` when Action Cable's engine was never required, because the namespace exists only once the engine assigns it; - `bin/rails initializers | grep action_cable` comes back empty for a skipped framework. ## Why it matters in practice - **Smaller apps.** API-only or worker apps drop frameworks they never use, which trims boot work and memory. - **Readable dependencies.** The explicit list documents what the app relies on better than `rails/all`. - **Upgrade safety.** When a new framework appears in a future `rails/all`, an explicit list does not pick it up silently. - **Debugging.** `bin/rails initializers` prints each initializer as `ClassName.initializer_name`, so you can see which railtie contributes what.

  • Why are Action Cable, Action Mailbox, Action Text and Active Storage engines rather than plain railties?
    They ship application code the host uses directly: models, controllers, views, routes or migrations. A `Rails::Engine` adds those `app/` paths, routes and migration paths to the host; a plain `Rails::Railtie` can only add initializers, configuration, tasks and generators.
  • What happens to a leftover config.action_mailer line after removing action_mailer/railtie?
    Boot fails with `NoMethodError`. The `action_mailer` key existed only because `ActionMailer::Railtie` assigned it as an `ActiveSupport::OrderedOptions`; without that class, the configuration's `method_missing` has no key to return.

  • rails/all loads every framework's classes eagerly at boot
  • Action Cable is a plain railtie with no app code
  • Commenting out a framework's require leaves its config namespace usable
  • Rails core initializes Active Record itself without a railtie
  • Gems' railties load before the frameworks in rails/all

Assumes Rails 8.1, whose rails/all requires ten railties and engines including action_mailbox/engine and action_text/engine; the generator writes explicit requires when a framework is skipped.