Rails Loading and Execution

All problems in computer science can be solved by another level of indirection.

-- Butler Lampson

Rails uses many levels of indirection, sometimes rewriting the rules on the fly. This page covers the very tip of the iceberg...

Note: The information below is based on the default configuration I saw in Rails 2.0.2; for any other configuration, Your Mileage May Vary... More generally, I pieced the following information together from a variety of sources and a bit of experimentation, so (a) some of it may not be common knowledge and (b) some of it may be incomplete (or worse, incorrect). Please enjoy the former and let me know about the latter...

Common Start-up Code

config/boot.rb (1)

The first time that boot.rb is loaded, after defining assorted methods, it "reboots" the app. If a file named config/preinitializer.rb is present, it is loaded. boot.rb then selects and runs the appropriate version of Rails (ie, GemBoot, VendorBoot).


environment.rb loads boot.rb a second time, then (optionally) defines some environmental settings. Because of the rebooting process, definitions from the first loading of boot.rb are not available in the first part of environment.rb. After the second loading of boot.rb, however, they are.

config/boot.rb (2)

The second time that boot.rb is loaded (by environment.rb), it merely defines assorted methods.


The specific name of this file is {development,production,test}.rb, depending on the setting of the RAILS_ENV environment variable.


Each plugin's init.rb file is loaded, in sequence, ordered by the plugin's name. Note that an init.rb file may well cause other files to be loaded, either immediately or later (via some form of callback). See Rails Plugins for details.


The routes.rb file will be loaded, controlling the interpretation of the incoming URL. See Rails Routes for details.


The inflections.rb file is the appropriate place to add pluralization rules.


The mime_types.rb file is the appropriate place to add MIME types.


The application.rb file is the appropriate place to add filters that apply to all of the app's controllers. It can also define methods (etc) for the app's controllers (and views).


The application_helper.rb file is the appropriate place to add methods that are intended for use by all of the app's views.


Any other *_helper.rb files are loaded, in name order. These files are the appropriate place to add methods for use by particular views.

Note: If a method abc() is defined in more than one *_helper.rb file, the last definition loaded will "win". So, be sure to tie method definitions to particular classes (eg, FooHelper).

Fielding a URL (eg, .../foo/bar)

Normally, the URL comes in as part of an HTTP request. It is received by a Web server (eg, Apache, Mongrel) and handed to one of public/dispatch.* for processing.


Each of these files does some pre-processing (eg, requiring files, tweaking the load path). The public/dispatch.fcgi file then calls "RailsFCGIHandler.process!". Tracing things along that path is left as an exercise for the reader.

The public/dispatch.{cgi,rb} routines, however, call the Dispatcher.dispatch() method. Conveniently, this can be spoofed in script/console, as:

>> ENV['REQUEST_URI']    = '/foo/bar'
=> "/foo/bar"
>> ENV['REQUEST_METHOD'] = 'get'
=> "get"
>> Dispatcher.dispatch


The foo_controller.rb file is generally the appropriate place to add methods to the FooController class.


If present, the bar() method is run.


It is quite likely that the Controller will use one or more model methods. The definition file for each model is loaded, as needed, to make these methods available.


By default (ie, if bar() hasn't changed things) any existing bar.html.erb page is loaded.


    • Chapter 2: Working with Controllers
    • Chapter 3: Routing
    • Chapter 4: REST, Resources, and Rails
    • Chapter 5: Reflections on Rails Routing
    • Chapter 10: ActionView
    • Chapter 11: All About Helpers

This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r8 - 20 Jul 2008, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email