Reloading problem in development

I just upgraded from 1.2.3 to 2.1.0 and now I’m seeing a model reloading
problem in development. If I set config.cache_classes = true, the
problem goes away (which obviously isn’t a very good workaround in
development).

I get errors like this for nested associations called in a controller
(ie, :include => {:team_players => :person}):

 Association named 'person' was not found; perhaps you misspelled

it?

I load the page the first time and it works fine. Subsequent page loads
fails.

I’ve also found that I can’t call on an association in a View that
wasn’t preloaded in the controller.

So if I do this in the controller:

@teams = Ball::Team.find(:all, :conditions => “active = 1”, :include =>
:team_players)

it will not let me do this:

<% for t in @teams %>
Team: <%= t.name %>

<% for tp in t.team_players %>
Player name: <%= tp.person.full_name %> <!-- it croaks here on the
person association ->
<% end %>
<% end %>

The only real complexity with my configuration is that my models are in
subdirectories (so Ball::Team is in models/ball/team.rb). I set this in
my environments.rb file when I was running 1.2.3:

config.load_paths += Dir[RAILS_ROOT + ‘/app/models/*/’]

But now with rails 2.1.0, it isn’t working. Again, setting
config.cache_classes to true makes the problem go away, but I’d rather
downgrade to 1.2.6 than restart my mongrels every time i make a change.

On 2 Jul 2008, at 13:46, Robbie A. wrote:

I just upgraded from 1.2.3 to 2.1.0 and now I’m seeing a model
reloading
problem in development. If I set config.cache_classes = true, the
problem goes away (which obviously isn’t a very good workaround in
development).

Do you require models explicitly (this can confuse the dependencies
stuff). Do you use any plugins that hang on to model classes (or
instances of them) ?

Fred

Hi Robbie!

On Wed, Jul 2, 2008 at 7:46 AM, Robbie A. <

Hi Robbie:

(last message got cut off somehow.)

Can you try to upgrade from 1.2.3 to 1.2.6 and run your tests? 1.2.6
posts
lots of deprecation warnings and is meant to be a transitional version.
You
might find something useful there.

I suspect that the load-paths thing you’re doing is causing the problem.
The
first thing I’d do is explicitly add each model subfolder instead of *
and
see if that works. If it does, then perhaps this patch addresses it.

I don’t typically do this in my models folder - I’m pretty content with
a
flat folder structure, even with +100 models.

Do you require models explicitly (this can confuse the dependencies
stuff).

No.

Do you use any plugins that hang on to model classes (or
instances of them) ?

Fred

I’m using several external plugins. How would I find out? Did the
behavior for how this is handled change between 1.x and 2.x?

Regards,
Robbie
http://statsheet.com

Brian H. wrote:

Hi Robbie:

Hi Brian. I hope you are doing well.

(last message got cut off somehow.)

Can you try to upgrade from 1.2.3 to 1.2.6 and run your tests? 1.2.6
posts lots of deprecation warnings and is meant to be a transitional version.
You might find something useful there.

Just tried that and no errors. It worked fine, just like 1.2.3.

I suspect that the load-paths thing you’re doing is causing the problem.

That is the suggested solution if you have models in subdirectories.

The first thing I’d do is explicitly add each model subfolder instead of *
and see if that works. If it does, then perhaps this patch addresses it.

Tried it, no difference.

Dependencies move to ActiveSupport::Dependencies missed a few spots · rails/rails@cce30f7 · GitHub

I applied it, but it started complaining. It looks like other patches
are required before applying that patch.

I don’t typically do this in my models folder - I’m pretty content with
a flat folder structure, even with +100 models.

I have 232 models and counting. It is a rather large app that is
naturally hierarchical and using subdirectories made logical sense.
Unfortunately Rails doesn’t handle it very well.

Any other suggestions? :slight_smile:

Regards,
Robbie
http://statsheet.com

Here is a simple example of what I’m talking about.

My first example uses Rails 1.2.6:

script/console
Loading development environment.

conf.return_format = “”
cs = Ball::CoachSeason.find(:first)
puts cs.coach.full_name
David Aaron

reload!
Reloading…

cs = Ball::CoachSeason.find(:first)
puts cs.coach.full_name
David Aaron

Second example uses 2.1.0:

script/console
Loading development environment (Rails 2.1.0)

conf.return_format = “”
cs = Ball::CoachSeason.find(:first)
puts cs.coach.full_name
David Aaron

reload!
Reloading…

cs = Ball::CoachSeason.find(:first)
puts cs.coach.full_name
NoMethodError: undefined method full_name' for #<Ball::Coach:0x2ab4ce75fbe0> from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:256:inmethod_missing’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:177:in
send' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:177:inmethod_missing’
from (irb):7

On 2 Jul 2008, at 15:30, Robbie A. wrote:

.

I don’t typically do this in my models folder - I’m pretty content
with
a flat folder structure, even with +100 models.

I have 232 models and counting. It is a rather large app that is
naturally hierarchical and using subdirectories made logical sense.
Unfortunately Rails doesn’t handle it very well.

Probably not relevant to the question at hand, but you can stick your
models in separate folders without namespacing them:
http://blog.hasmanythrough.com/2008/5/6/a-simple-alternative-to-namespaced-models

Fred

On 2 Jul 2008, at 14:25, Robbie A. wrote:

I’m using several external plugins. How would I find out? Did the
behavior for how this is handled change between 1.x and 2.x?

Plugins used to be reloaded with each request by default, but they
aren’t anymore. I can’t remember when this change happened to be
honest. I have a feeling it was in 1.2 but it could have been 2.0
I’ve got some info on making plugins be reloaded on each request here

, you could try that out to exclude it as a source of problems
(alternatively examine Dependencies.load_once_paths in your 1.2 app:
if it looks the same there as in 2.0 then that’s probably not the
problem)

Fred

On 2 Jul 2008, at 15:43, Robbie A. wrote:

Here is a simple example of what I’m talking about.

Anything interesting in the models ? Have you been able to construct a
small app from scratch that exhibits the problem ?

Fred

Ahhh! I didn’t see the namespacing that you’re doing. I recommend
Fred’s
suggestion. Namespacing of models seems to get broken a lot. I’ve done
Rails
for over 3 years now and it’s been broken at least twice before (that I
remember). That doesn’t excuse it breaking of course.

Here’s an idea… can you run on edge and see it that fixes it?

On Wed, Jul 2, 2008 at 9:55 AM, Frederick C.
[email protected]

Thanks to Fred and Brian’s suggestions, I was able to track this down.
For some unknown reason I had put “include Ball” (using my previous
example) in lib/search.rb. After I removed it, the reload problem went
away.

Strange, but whatever :wink: I’m just glad it is fixed.