Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Initialization Order

Can someone please point me in the right direction for the order in which rails modules get instantiated.

The main things I'm trying to find are:

1) When do gems get loaded?

2) When do config/initializers/* get loaded?

3) When do named routes in routes.rb get processed?

like image 354
Dex Avatar asked Nov 07 '10 01:11

Dex


People also ask

What is Initializers in Ruby on Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

What are Railties?

Rails::Railtie is the core of the Rails framework and provides several hooks to extend Rails and/or modify the initialization process. Every major component of Rails (Action Mailer, Action Controller, Active Record, etc.) implements a railtie. Each of them is responsible for their own initialization.

How do I start Rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .


3 Answers

Answering your question is easy by adding some puts statements in your Rails application. (It seems like a lot of people are reluctant to dig in and do this, but I really recommend it!) So, by experimentation and observation alone, here is the order of the things you mentioned:

  1. boot.rb
  2. config/initializers/*
  3. routes.rb

Here is a little more detail:

1. boot.rb

This loads the application gems by using bundler:

require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

2. config/initializers/*

They run in alphabetical order.

If you are curious what triggers this, take a look at engine.rb in the railties source code. (It is useful to know that a Rails Application is a subclass of a Rails Engine.)

initializer :load_config_initializers do
  config.paths["config/initializers"].existent.sort.each do |initializer|
    load(initializer)
  end
end

3. routes.rb

By observation, I see that route drawing (specification) occurs next.

But looking at the details is more involved, so if you are interested I would read SO: Controlling routes loading order from Engines and perhaps take a look at the :add_routing_paths initializer in engine.rb.

like image 104
David J. Avatar answered Oct 21 '22 11:10

David J.


Check out this insanely detailed (and long) piece of documentation on the initialization process:

http://guides.rubyonrails.org/initialization.html

like image 45
johnmcaliley Avatar answered Oct 21 '22 12:10

johnmcaliley


I started a console in Rails 3 and here is the order:

  • script/rails
  • config/boot.rb
  • config/application.rb
  • config/environment.rb
  • config/initializers/*.rb (In alphabetic order)
like image 2
jef Avatar answered Oct 21 '22 11:10

jef