# A Brief Introduction to Rails Initializers: Why, What, and How

*Published on October 19, 2024*

Tags: Rails Internals

---


Initializers are an important concept in Rails, but I couldn't find much information about them online, other than&nbsp;[the official Rails guides](https://guides.rubyonrails.org/configuring.html#using-initializer-files). Recently, I did a deep dive into the Rails initialization process, and what follows is everything I learned about initializers.

### What we'll learn:

- What are Initializers?
- Why use an initializer?
- How Rails loads the initializers?
- How Rails executes an initializer?

By the end of the article, I hope you'll have a much better understanding of initializers and an appreciation of all the things that Rails does behind the scenes to not only make it work, but also make it look like magic!

Let's begin...

## What are Initializers?

An initializer is simply a piece of Ruby code under the&nbsp;`config/initializers`&nbsp;directory that you can use to configure your Rails application or external gems.

To create a new initializer, add a Ruby file in the&nbsp;`config/initializers`&nbsp;directory. Rails will run the initializers after loading the framework and any other gems used in your application.

![rails-initialization-sequence.png](https://writesoftwarewell.com/media/nmzc31j4sgblr0xzb24atpyc97fb.png "Rails Initialization Sequence")

As an example, consider the&nbsp;`filter_parameter_logging`&nbsp;initializer that Rails provides out-of-box. You can use it to&nbsp;[hide sensitive data from the log file](/how-parameter-filtering-works-in-rails). This prevents sensitive data such as credit card numbers or auth keys from accidentally leaking into the log files.

```ruby
# config/initializers/filter_parameter_logging.rb

Rails.application.config.filter_parameters += [ :password, :secret, :token ]
```

Note that it's a plain Ruby file, and you have access to the Rails application and configuration objects.

**Configuring these values inside an initializer provides a single place where you can add/remove them and avoids having to mix this functionality into the application code.**

P.S. To learn how parameter filtering works in Rails, check out this article: [Hide Sensitive Information from Logs using Parameter Filtering](/how-parameter-filtering-works-in-rails)

## Why use an Initializer?

You can use initializers to configure your Rails application or external gems. Initializers allow you to provide configuration settings that should be made after all of the frameworks and gems are loaded, such as options to configure their settings.

> **Instead of writing the one-time initialization logic in your application code, use an initializer.**

You can also use an initializer to set default values for your application and external gems. For example, here's the&nbsp;[`Delayed_Job`](https://github.com/collectiveidea/delayed_job)&nbsp;initializer setting a bunch of useful defaults.

```ruby
# config/initializers/delayed_job_config.rb

Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes
Delayed::Worker.default_queue_name = 'default'
Delayed::Worker.delay_jobs = !Rails.env.test?
```

I also asked about the different&nbsp;[use-cases for Rails initializers](https://www.reddit.com/r/rails/comments/z09eq8/use_cases_for_rails_initializers/)&nbsp;on Reddit and received a bunch of great answers. Here're the main reasons people use initializers:

1. To set up one-time operations, like configuration of external gems
2. Anything that needs to happen before your app starts running.
3. Add monkey-patching code to overwrite a library you‘re using.

**Rails ensures that the initializer code is loaded only once on startup and before receiving the first request.**

If you know other use cases for them, please let me know in the comments below.

## How Does Rails Load the Initializers?

> For the section that follows, I'd highly recommend that you open the Rails codebase, put a breakpoint, and follow the code path with me. To learn more about stepping through Rails, check out:&nbsp;[How to Debug and Step-Through the Rails Codebase](/how-to-debug-and-step-through-rails-source-code)

At this point, you might be wondering:&nbsp;**How does Rails picks up these Ruby files under**&nbsp;**`config/initializers`**&nbsp;**directory, and runs them when the application starts?**

It does seem like magic, doesn't it?

**The answer is: Code that loads the initializer scripts is an initializer itself, defined by the framework.**

The initializer files in&nbsp;`config/initializers`&nbsp;(and any subdirectories of&nbsp;`config/initializers`) are sorted and loaded one by one as part of the&nbsp;`load_config_initializers`&nbsp;initializer.

```ruby
# railties/lib/rails/engine.rb

module Rails
  class Engine < Railtie
    initializer :load_config_initializers do
      config.paths["config/initializers"].existent.sort.each do |initializer|
        load_config_initializer(initializer)
      end
    end
  end
end
```

In fact, the above snippet shows the other way to create an initializer using the&nbsp;`initializer`&nbsp;method. Let's take a look.

### The initializer method

The&nbsp;`initializer`&nbsp;method comes from the&nbsp;`Initializable`&nbsp;module, which is included by&nbsp;`Rails::Railtie`, the superclass of&nbsp;`Rails::Engine`&nbsp;class.

This method simply stores the passed blocks into a collection of initializers. Here's a simplified implementation:

```ruby
module Initializable
  def self.initializer(name, opts = {}, &blk)
    initializers << Initializer.new(name, &blk)
  end
end
```

**It takes three arguments:**

1. The name for the initializer.
2. An options hash. The&nbsp;`:before`&nbsp;key in the options hash can be specified to specify which initializer this new initializer must run before, and the&nbsp;`:after`&nbsp;key will specify which initializer to run this initializer&nbsp;_after_.
3. A block. It's a piece of code to execute when the initializer runs.

Initializers defined using the&nbsp;`initializer`&nbsp;method will be run in the order they are defined in, with the exception of ones that use the&nbsp;`:before`&nbsp;or&nbsp;`:after`&nbsp;methods.

### Out-of-box initializers

Rails provides several initializers out-of-box that run on startup that are all defined using the&nbsp;`initializer`&nbsp;method. Here's an example of the&nbsp;`assets_config`&nbsp;initializer from Action Controller.

```ruby
module ActionController
  class Railtie < Rails::Railtie
    initializer "action_controller.assets_config", group: :all do |app|
      app.config.action_controller.assets_dir ||= app.config.paths["public"].first
    end
  end
end
```

You can find&nbsp;[a comprehensive list of all initializers in Rails](https://guides.rubyonrails.org/configuring.html#initializers)&nbsp;on the official guides on&nbsp;[configuring Rails applications](https://guides.rubyonrails.org/configuring.html).

**But defining an initializer is only half of the process. How do all these initializers get executed? Who executes them?**

Keep on reading...

## Executing Initializers: Rails Initialization Process

So far, we've only seen how to create initializers. However, simply defining an initializer is not enough. The initialization code needs to run for it to do something useful.

This section answers the question:&nbsp;**How Rails executes the initializers?**

What follows is a detailed explanation of the Rails initialization process, where Rails takes all the initializers that are included out-of-box, or those that you've provided, and executes them sequentially.

Here's a high-level sequence diagram you can refer to while reading the rest of the article.

![rails-initialization-process.png](https://writesoftwarewell.com/media/ft84l2pvzfsbu2ei4u7v5uqryva3.png "Rails Initialization Process")

All Rails applications use the&nbsp;`config.ru`&nbsp;file. This file is used by Rack-based servers, such as Puma, to launch the server and start the application.

> To learn more about Rack and config.ru file, check out&nbsp;[my detailed article on Rack](/definitive-guide-to-rack).

**Step 1:**&nbsp;The&nbsp;`config.ru`&nbsp;file requires the&nbsp;`config/environment`&nbsp;file.

```ruby
# config.ru

require_relative "config/environment"
```

**Step 2:**&nbsp;The environment file loads the Rails application and initializes it using the&nbsp;`Rails.application.initialize!`&nbsp;method.

```ruby
# config/environment.rb

require_relative "application"

Rails.application.initialize!
```

**Step 3:**&nbsp;The&nbsp;`Rails.application`&nbsp;points to your application's instance, defined in the&nbsp;`config/application.rb`&nbsp;file. The&nbsp;`Application`&nbsp;class inherits from the&nbsp;`Rails::Application`&nbsp;class, which provides the&nbsp;`initialize!`&nbsp;method.

```ruby
# config/application.rb

module Blog
  class Application < Rails::Application

  end
end

# railties/lib/rails/application.rb

module Rails
  class Application < Engine
    def initialize!(group = :default)
      run_initializers(group, self)
    end
  end
end
```

The&nbsp;`initialize!`&nbsp;method calls this&nbsp;`run_initializers`&nbsp;method, as seen above. This is the method that grabs all the initializers and sequentially runs them.

However, you don't see the&nbsp;`run_initializers`&nbsp;method in the&nbsp;`application.rb`&nbsp;file. Where does it come from?

**Answer:**

- The&nbsp;`Rails::Application`&nbsp;class inherits from&nbsp;`Rails::Engine`, which inherits from&nbsp;`Rails::Railtie`.
- The&nbsp;`Rails::Railtie`&nbsp;class includes the&nbsp;`Rails::Initializable`&nbsp;module defined in the&nbsp;`railites/lib/rails/initializable.rb`&nbsp;file.
- The&nbsp;`Rails::Initializable`&nbsp;module provides the&nbsp;`run_initializers`&nbsp;method, which is included in your application class.

Here's the inheritance hierarchy of these classes.

![rails-application-inheritance.png](https://writesoftwarewell.com/media/3uk3y6g47kzogbvv42dtha0oqoh8.png "Rails Application Inheritance Hierarchy")

Here's a simplified implementation of the&nbsp;`run_initializers`&nbsp;method.

```ruby
# railties/lib/rails/initializable.rb

def run_initializers(*args)
  initializers.each do 
    initializer.run(*args)
  end
end
```

> In reality, it topologically sorts the initializers graph, based on the&nbsp;`:before`&nbsp;and&nbsp;`:after`&nbsp;options, and then runs them in order. But that's a topic for another blog post.

And that's how Rails executes the initializers to configure your application and other gems. Once all the initializers have run, your application is ready to handle incoming requests.

## Summary

- An initializer is a piece of Ruby code under the&nbsp;`config/initializers`&nbsp;directory. You can use initializers to configure your Rails application or external gems.
- Rails makes sure that the initializer code is loaded during the initialization process, after loading the framework and any other gems, and before receiving the first request.
- Instead of writing the one-time initialization logic in your application code, you should use an initializer.

