# STI include in find

**URL:** <https://www.ruby-forum.com/t/sti-include-in-find/172455>\
**Category:** Rails\
**Created:** [July 21, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/sti-include-in-find/172455 "2009-07-21T21:36:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 21, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/sti-include-in-find/172455/1 "2009-07-21T21:36:34Z")

</div>

Hi,

I am using single table inheritance in my application as follows:

class Company \< ActiveRecord::Base  
…  
end

class Member \< Company  
…  
end

class Applicant \< Company  
…  
end

class Application \< ActiveRecord::Base  
has\_one :applicant  
has\_one :member  
…  
end

I wish to use an include statement when I do a find on the Application  
(ie. Application.find(:all, :include =\> [:member]). Is there a way to  
accomplish this. I’m assuming it is not working because of the STI.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 21, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/sti-include-in-find/172455/2 "2009-07-21T23:06:02Z")

</div>

Hey Lundie,

That’s odd…this sounded weird to me so I setup a sample app and tested  
it.  
It works as expected (note the 2 mysql calls the find generates, as you  
would expect):

# ActiveRecord#explain is added by the (very young) explain-query gem

> > Post.explain do  
> > ?\> Post.find :first, :include =\> [:user]
> 
> > end  
> > SELECT \* FROM `posts` LIMIT 1  
> > select\_type | key\_len | table | id | possible\_keys | type | Extra |  
> > rows  
> > | ref | key

* * *

SIMPLE | | posts | 1 | | ALL | | 6  
| |

SELECT `entities`.\* FROM `entities` WHERE (`entities`.post\_id = 2) AND  
(  
(`entities`.`type` = ‘User’ ) )  
select\_type | key\_len | table | id | possible\_keys | type |  
Extra | rows | ref | key

* * *

SIMPLE | | entities | 1 | | ALL | Using  
where | 2 | |

=\> #\<Post id: 2, title: “First Post”, content: “some content\r\n”,  
created\_at: “2009-07-07 09:19:46”, updated\_at: “2009-07-20 14:16:16”,  
permalink: “first-post”, comments\_count: 10\>

Is your STI working correctly in other use cases?

Regards,  
Gustav P.
