Initialization: the layers, the order, where to put your code

Hey all, I hope it’s not a problem that I’m jumping in here after May but I thought of a topic in Rails that I suspect causes people a lot of difficulties when customizing and extending: initialization

There’s a number of layers of initialization as far as I can tell:

  • Everything that runs inside an Application, Engine or Railtie
  • The contents of the initializer blocks inside an Application, Engine or Railtie.
  • The methods in Rails::Railtie::Configuration
  • The intializers in config/initializers
  • ActiveSupport::on_load hooks

Understanding how all of those fit together is quite complex because there’s almost no documentation or guidance on their use so I’m left with a lot of questions.

  • What is the actual order of loading when taking all of these layers into account?
  • If I need something available for a rake task, where does it need to go?
  • What about ordering parts of the initialization process so I don’t need to worry about the order gems are in my Gemfile?
  • What parts of my application, like models, are loaded and available at a given point in the initialization process?

Some sort of guide on how to use the layers, how they work together and where various types of initialization should go, i.e. If you want to add x, you should put it in this part of the process, would really make extending Rails properly far easier and enjoyable.

1 Like

Do those links help in any way to answer some of your questions?

We don’t document much about this in the guides because we don’t think there are many reasons to change the initialization process of applications, so let me try to get to the bottom of the issue. If you feel the need for documentation about this, can you explain what exactly you are trying to archive and what kind of problems you encounter in you day-by-day that cause you to look for documentation about the initialization process?

4 Likes

Thank you @rafaelfranca, this helps greatly. All of my searches did not find the Rails::Application documentation as the appropriate documentation for this.

I’ll submit a PR to add links to the relevant RoR Guide which I think will improve visibility.

1 Like