15

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?

Dex
  • 12,527
  • 15
  • 69
  • 90

4 Answers4

13

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.

Community
  • 1
  • 1
David J.
  • 31,569
  • 22
  • 122
  • 174
9

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

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

johnmcaliley
  • 11,015
  • 2
  • 42
  • 47
  • 2
    The usual place for Rails Guide ( [guides.rubyonrails.org](http://guides.rubyonrails.org/index.html)) seems like it has a slightly newer version of the [The Rails Initialization Process guide](http://guides.rubyonrails.org/initialization.html). It is probably better to link there since it has all the other guides (the ryanbiggs.com site only has the initialization guide). – Chris Johnsen Nov 07 '10 at 04:02
  • Thanks, answer updated. I didn't even realize that it was in Rails Guides now. – johnmcaliley Nov 08 '10 at 18:23
  • 3
    @cowboycoded you might want to remove the link to your blog post as it's no longer active – absessive Jun 05 '14 at 15:42
2

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)
Outside_Box
  • 447
  • 1
  • 4
  • 16
jef
  • 51
  • 1
  • 5
-5

I really don't know but logically in this order

  1. gems
  2. initializers
  3. routes

1) The only way I know how to edit gems is by editing the source file themselves. Moreover rails itself and basically everything with rails is a gem so I'm sure they are loaded first

2) Initializers are probably loaded second, or at least considering the three things you mentioned, because they might load information or modules that routes with resources and associations need.

3) Process of elimination


In terms of inner order it's probably abc

Don't take this seriously though :)

thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Well meant, but not very helpful, there are other questions who cover this in depth. Did you consider deleting your answer? – reto Sep 10 '13 at 13:21