# Did the order of autoloading assets change in Rails 2.1?

**URL:** <https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299>\
**Category:** Rails\
**Created:** [June 12, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299 "2008-06-12T19:14:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/1 "2008-06-12T19:14:43Z")

</div>

All,

I upgraded to Rails 2.1 from 2.0.2 and my app. started to show problems  
with not having access to certain constants, etc. I have several  
“requires” set up in environment.rb so that various modules will be  
available for the app.

I’ve played with it some, and the best theory that I can come up with so  
far is that the RAILS\_ROOT/lib modules are now loaded before  
environment.rb is executed?

Were there any major changes to boot.rb from 2.0.2 to 2.1?

Confusing.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/2 "2008-06-12T19:40:16Z")

</div>

My problem appears to be that I have a library in RAILS\_ROOT/lib  
(cache\_manager.rb) that is _definitely_ being loaded _before_  
config/environment.rb is being executed.

Since the requisite requires to allow this library to load successfully  
are in environment.rb, and it hasn’t been executed yet, the file won’t  
load.

So this leads me to two questions:

1. What causes the RAILS\_ROOT/lib files to be loaded when you start up  
Rails?

2. Did this used to occur _after_ the execution of environment.rb but  
now it doesn’t?

I recognize that there may be something in my app. that is causing the  
library to be loaded, but I haven’t found it yet, and of course, this  
only started happening when I moved to 2.1, so I’m thinking it’s a  
framework issue.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/3 "2008-06-12T19:55:49Z")

</div>

ANSWER:

I had requires that were needed for libraries in RAILS\_ROOT/lib at the  
bottom of my environment.rb file.

When I moved these requires to _before_ the initializer block in  
environment.rb, everything worked fine.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/4 "2008-06-12T19:46:45Z")

</div>

On 12 Jun 2008, at 18:40, Wes G. wrote:

> So this leads me to two questions:
> 
> 1. What causes the RAILS\_ROOT/lib files to be loaded when you start up  
> Rails?  
> I’d be surprised if anything in there is loaded systematically
> 
> 2. Did this used to occur _after_ the execution of environment.rb but  
> now it doesn’t?
> 
> I recognize that there may be something in my app. that is causing the  
> library to be loaded, but I haven’t found it yet, and of course, this  
> only started happening when I moved to 2.1, so I’m thinking it’s a  
> framework issue.  
> stick a breakpoint on the first line of cache\_manager and see what the  
> stack trace looks like ?

Fred

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/5 "2008-06-12T23:25:52Z")

</div>

Wes G. wrote:

> environment.rb.  
> OR
> 
> 1. Require in those lower level dependencies before the  
> “Rails::Initializer.run” block in environment.rb.
> 
> Wes

THE FINAL ANSWER: 😉

1. create an /initializers folder inside RAILS\_ROOT/config
2. create a application.rb file inside that folder and move all your  
‘requires’ into it.

If you create a new rails project under 2.1 and look at what it does in  
your environment.rb and in the initializers folder you’ll see the Rails  
2.1 way this kind of this is supposed to be handled.

My understanding is that you can create any/as many .rb files inside of  
initializers that you want (to separate different needs) and they’ll  
all be loaded. So, you could have an initializers/foo\_feature.rb and an  
initializers/bar\_extensions.rb, etc, etc, etc.

–

[http://www.5valleys.com/](http://www.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/6 "2008-06-12T20:10:02Z")

</div>

THE REAL ANSWER:

application.rb is now loaded as part of the call to the  
“Rails::Initializer.run” block in environment.rb, and it wasn’t in  
pre-2.1 versions of Rails.

In this case, application.rb had dependencies that is was requiring.

These libraries, which were require’d in application.rb in turn had  
dependencies that were not being “required in” until the end of  
environment.rb.

Before this worked because environment.rb would be run before  
application.rb (the first time application.rb would be loaded was on the  
first request).

But now, if application.rb needs anything to be loaded, you must either:

1. Ensure that the full dependency tree is satisfied in application.rb  
itself

OR

1. Require in those lower level dependencies before the  
“Rails::Initializer.run” block in environment.rb.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 12, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/did-the-order-of-autoloading-assets-change-in-rails-2-1/140299/7 "2008-06-12T23:34:42Z")

</div>

Jon G. wrote:

> THE FINAL ANSWER: 😉
> 
> 1. create an /initializers folder inside RAILS\_ROOT/config
> 2. create a application.rb file inside that folder and move all your  
> ‘requires’ into it.
> 
> If you create a new rails project under 2.1 and look at what it does in  
> your environment.rb and in the initializers folder you’ll see the Rails  
> 2.1 way this kind of this is supposed to be handled.
> 
> My understanding is that you can create any/as many .rb files inside of  
> initializers that you want (to separate different needs) and they’ll  
> all be loaded. So, you could have an initializers/foo\_feature.rb and an  
> initializers/bar\_extensions.rb, etc, etc, etc.

This is a good final answer. I already have 3 initializer files for  
various pieces of Rails that I’m monkey patching, and this makes sense  
as well.

Too bad that this change in application.rb processing didn’t happen in  
2.0 when I was having to hack all over my environment.rb file anyway :).

WG
