Did the order of autoloading assets change in Rails 2.1?

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

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

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?

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

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

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

2) Require in those lower level dependencies before the
"Rails::Initializer.run" block in environment.rb.

Wes

Wes Gamble wrote:

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

2) Require in those lower level dependencies before the "Rails::Initializer.run" block in environment.rb.

Wes

THE FINAL ANSWER: :wink:

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.

Jon Garvin wrote:

THE FINAL ANSWER: :wink:

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