# Why must I save/reload to get grandchildren in STI?

**URL:** <https://www.ruby-forum.com/t/why-must-i-save-reload-to-get-grandchildren-in-sti/70237>\
**Category:** Rails\
**Created:** [September 27, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/why-must-i-save-reload-to-get-grandchildren-in-sti/70237 "2006-09-27T18:21:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [September 27, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/why-must-i-save-reload-to-get-grandchildren-in-sti/70237/1 "2006-09-27T18:21:26Z")

</div>

I have the following models:

Person \< ActiveRecord::Base  
end

Contact \< Person  
belongs\_to :company  
end

Primarycontact \< Contact  
belongs\_to :company  
end

Company \< ActiveRecord::Base  
has\_many :contacts  
has\_one :primarycontact  
end

I have already saved these objects in the database, verified in mysql  
client:  
(pseudocode)

a =\> company  
joe =\> primarycontact

When I try to access them this way:

joe.name =\> ‘joe’ #yep, that’s right  
a.primarycontact =\> joe #good, that’s what I expect  
a.contacts.each {|c| puts c.name} =\> nil #why?  
a.save =\> true  
a.contacts(:reload =\> true).each {|c| puts c.name} =\> ‘joe’ #huh?

Now I can shut down the console, start it up again, and do the whole  
dance again from the top. Why to I have to save / reload every time I  
want to get at grandchildren? It’s not as though I’m actually altering  
the data in the database. It seems very inefficient.

Any ideas or help you may have would be greatly appreciated.

Thanks,  
Eric

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [September 27, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/why-must-i-save-reload-to-get-grandchildren-in-sti/70237/2 "2006-09-27T21:50:00Z")

</div>

I found the solution.

By adding model declarations in application.rb, rails became aware of  
the grandchild class Primarycontact from the start, and everything  
works fine. ( e.g. model :primarycontact )

My little save /reload dance just tipped rails off that the class  
existed so that it could load it’s definition. Explicitly loading the  
definition in application.rb solves the problem.

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [September 28, 2006, 7:13am UTC](https://www.ruby-forum.com/t/why-must-i-save-reload-to-get-grandchildren-in-sti/70237/3 "2006-09-28T07:13:04Z")

</div>

Eric ïèøåò:

> I found the solution.
> 
> By adding model declarations in application.rb, rails became aware of  
> the grandchild class Primarycontact from the start, and everything  
> works fine. ( e.g. model :primarycontact )
> 
> My little save /reload dance just tipped rails off that the class  
> existed so that it could load it’s definition. Explicitly loading the  
> definition in application.rb solves the problem.

Yeah, that’s the problem with STI in development environment. On every  
request all reloadable classes are removed and loaded only on demand. So  
STI children and grandchildren could not be loaded on next request.  
Maybe, explicitly loading all models would solve this:

app/controllers/application.rb  
Dir[RAILS\_ROOT+’/app/models/\*.rb’].each { |f| require f }
