Rails Application Initialization in 20 Easy Steps

Rails::Initializer is the main class that handles setting up the Rails environment
within Ruby. Initialization is kicked off by config/environment.rb, which contains the

Rails::Initializer.run do |config|
# (configuration)


Rails::Initializer.run yields a new Rails::Configuration object to the block. Then
run creates a new Rails::Initializer object and calls its process method, which
takes the following steps in order to initialize Rails:

1. check_ruby_version: Ensures that Ruby 1.8.2 or above (but not 1.8.3) is being
2. set_load_path: Adds the framework paths (RailTies, ActionPack,* ActiveSupport,
ActiveRecord, Action Mailer, and Action Web Service) and the application’s
load paths to the Ruby load path. The framework is loaded from vendor/rails or
a location specified in RAILS_FRAMEWORK_ROOT.
3. require_frameworks: Loads each framework listed in the frameworks configura-
tion option. If the framework path was not specified in RAILS_FRAMEWORK_ROOT
and it does not exist in vendor/rails, Initializer will assume the frameworks are
installed as RubyGems.
4. set_autoload_paths: Sets the autoload paths based on the values of the load_
paths and load_once_paths configuration variables. These determine which paths
will be searched to resolve unknown constants. The load_paths option is the
same one that provided the application’s load paths in step 2.
5. load_environment: Loads and evaluates the environment-specific (development,
production, or test) configuration file.
6. initialize_encoding: Sets $KCODE to u for UTF-8 support throughout Rails.
7. initialize_database: If ActiveRecord is being used, sets up its database configu-
ration and connects to the database server.
8. initialize_logger: Sets up the logger and sets the top-level constant RAILS_
DEFAULT_LOGGER to the instance. If logger is specified in the configuration, it is
used. If not, a new logger is created and directed to the log_path specified. If that
fails, a warning is displayed and logging is redirected to standard error.
9. initialize_framework_logging: Sets the logger for ActiveRecord, ActionControl-
ler, and Action Mailer (if they are being used) to the logger that was just set up.
10. initialize_framework_views: Sets the view path for ActionController and Action
Mailer to the value of the view_path configuration item.
11. initialize_dependency_mechanism: Sets Dependencies.mechanism (which deter-
mines whether to use require or load to load files) based on the setting of the
cache_classes configuration item.
12. initialize_whiny_nils: If the whiny_nils configuration item is true, adds the
Whiny Nil extensions (that complain when trying to call id or other methods on
nil) to NilClass.

13. initialize_temporary_directories: Sets ActionController’s temporary session
and cache directories if they exist in the filesystem.
14. initialize_framework_settings: Transforms the framework-specific configura-
tion settings into method calls on the frameworks’ Base classes. For example,
consider the configuration option:
config.active_record.schema_format = :sql
The config.active_record object is an instance of Rails::OrderedOptions, which
is basically an ordered hash (ordered to keep the configuration directives in
order). During initialization, the initialize_framework_settings method trans-
forms it into the following:
ActiveRecord::Base.schema_format = :sql
This offers the advantage that the Configuration object doesn’t have to be
updated every time a framework adds or changes a configuration option.
15. add_support_load_paths: Adds load paths for support functions. This function is
currently empty.
16. load_plugins: Loads the plugins from paths in the plugin_paths configuration
item (default vendor/plugins). If a plugins configuration item is specified, load
those plugins respecting that load order. Plugins are loaded close to the end of
the process so that they can override any already loaded component.
17. load_observers: Instantiates ActiveRecord observers. This is done after plugins
so that plugins have an opportunity to modify the observer classes.
18. initialize_routing: Loads and processes the routes. Also sets the controller
paths from the controller_paths configuration item.
19. after_initialize: Calls any user-defined after_initialize callback. These call-
backs are defined in the configuration block by config.after_initialize { … }.
20. load_application_initializers: Loads all Ruby files in RAILS_ROOT/config/
initializers and any of its subdirectories. Old framework initialization that may
previously have been contained in config/environment.rb can now properly be
broken out into separate initializers.
Now the framework is ready to receive requests.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s