# STI and subclassing twice

**URL:** <https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488>\
**Category:** Rails\
**Created:** [May 8, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488 "2009-05-08T12:05:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [May 8, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488/1 "2009-05-08T12:05:35Z")

</div>

I’m trying using STI to implement a hiearchy similar to this:

script/generate scaffold name:string type:string

class Person \< ActiveRecord::Base; end  
class Customer \< Person; end  
class Employee \< Person; end  
class Developer \< Employee; end

I.e., there are two levels of inheritance (Developer \< Employee \<  
Person).

When creating a Customer, Rails understands it’s a Person, but if I  
create a Developer, Rails ‘tends to forget’ it’s an Employee. In other  
words:

$ script/console

> > Developer.create :name=\>‘Bob’  
> > =\> #\<Developer id: 1, name: “Bob”, type: “Developer”, created\_at:  
> > “2009-05-08 09:59:58”, updated\_at: “2009-05-08 09:59:58”\>
> 
> > exit

$ script/console

> > Employee.find :all  
> > =\> []
> 
> > Person.find :all  
> > =\> [#\<Developer id: 1, name: “Bob”, type: “Developer”, created\_at:  
> > “2009-05-08 09:59:58”, updated\_at: “2009-05-08 09:59:58”\>]

Worst of all, if I change the order of the ‘find’ statements, things  
work:

$ script/console

> > Person.find :all  
> > =\> [#\<Developer id: 1, name: “Bob”, type: “Developer”, created\_at:  
> > “2009-05-08 09:59:58”, updated\_at: “2009-05-08 09:59:58”\>]
> 
> > Employee.find :all  
> > =\> [#\<Developer id: 1, name: “Bob”, type: “Developer”, created\_at:  
> > “2009-05-08 09:59:58”, updated\_at: “2009-05-08 09:59:58”\>]

Maybe this has to do with some caching stuff… any ideas?

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [May 11, 2009, 9:52am UTC](https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488/2 "2009-05-11T09:52:48Z")

</div>

Any idea? I think there might be a bug here. I’m using Rails 2.3.2, but  
I get the same behaviour in Rails 2.2.2.

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [May 11, 2009, 10:38am UTC](https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488/3 "2009-05-11T10:38:18Z")

</div>

On May 11, 8:52 am, José Ignacio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Any idea? I think there might be a bug here. I’m using Rails 2.3.2, but  
> I get the same behaviour in Rails 2.2.2.

There’s some funky stuff with STI that basically boils down to the  
fact that if a class hasn’t been loaded yet rails doesn’t know it  
exists (and so doesn’t create the correct query).  
One way of doing things is to force these classes to be loaded, eg at  
the bottom of person.rb put

require\_dependency ‘customer’  
require\_dependency ‘employee’  
require\_dependency ‘developer’

or something like that.

Fred

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [May 11, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/sti-and-subclassing-twice/167488/4 "2009-05-11T12:08:26Z")

</div>

Interesting… That makes everything work as expected! Thanks

> There’s some funky stuff with STI that basically boils down to the  
> fact that if a class hasn’t been loaded yet rails doesn’t know it  
> exists (and so doesn’t create the correct query).  
> One way of doing things is to force these classes to be loaded, eg at  
> the bottom of person.rb put
> 
> require\_dependency ‘customer’  
> require\_dependency ‘employee’  
> require\_dependency ‘developer’
