# Eager loading issue

**URL:** <https://www.ruby-forum.com/t/eager-loading-issue/114524>\
**Category:** Rails\
**Created:** [September 13, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/eager-loading-issue/114524 "2007-09-13T21:56:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 13, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/eager-loading-issue/114524/1 "2007-09-13T21:56:48Z")

</div>

Hi,

so I have an eager loading issue I’d like to get opinions on. My models  
are imported from an xml file and I’m able to preserve the hierarchy and  
use the models to\_xml() method to get the whole tree back as an export.  
This all works fine. This is an example of structure:

Where I’m having an issue is eager loading of a manufacturer like so:

manufacturer=Manufacturer.find(1, :include=\>{:devices=\>:capabilities})

The above manufacturer find works the first time and is also a ‘root’  
manufacturer which all others can extend (this is how the project was  
given to me, I know…). This being the case, I chose to ‘cache’ the root  
manufacturer and to do that I put it in my ApplicationController class  
and set up a before\_filter which checks for a $root\_manufacturer global.  
If $root\_manufacturer isn’t found, then it’s loaded.

Like I said, this all works the first time. But if I try to use  
$root\_manufacturer.devices, I get the following error in my view:

ActionView::TemplateError undefined method `devices’ for  
#Manufacturer:0x35fc6e4

Specifically this line:

\<%root\_manufacturer.devices.each\_with\_index do |device,index|%\>

When I first loaded the manufacturer into $root\_manufacturer global and  
checked the length of its devices, it worked. But when I go to the page,  
it doesn’t. It seems like not very many people use eager loading - that  
I’ve found - so there’s not much out there on this issue. I’m trying to  
figure out if maybe for some reason, the devices attribute is falling  
off when I try to globalize the manufacturer\_root object. It’s strange  
too because on the view in the line before the error I have this:

\<% logger.debug “#{$root\_manufacturer.inspect}”%\>

and I can clearly see that \<Manufacturer:0x35fc6e4  
@devices=[#\<Device:0x35fc25c …] …\> exists. So I can’t figure out  
what’s going on. Any suggestions appreciated. TIA.
