# STI Troubles in a has\_many : belongs\_to relationship

**URL:** <https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891>\
**Category:** Rails\
**Created:** [October 12, 2006, 4:48am UTC](https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891 "2006-10-12T04:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [October 12, 2006, 4:48am UTC](https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891/1 "2006-10-12T04:48:00Z")

</div>

I’m having difficulty with single table inheritance when the STI table  
is the “belongs\_to” in a has\_many / belongs\_to relationship. Here’s  
what the models look like (contrived and simplified) :

pie\_lover.rb  
class PieLover \< ActiveRecord::Base  
has\_many :pies  
end

pie.rb  
class Pie \< ActiveRecord::Base  
belongs\_to :pie\_lover  
end

cherry\_pie.rb  
class CherryPie \< Pie  
end

apple\_pie.rb  
class ApplePie \< Pie  
end

Can someone help me understand why:

This works:  
CherryPie.count

And this works:  
PieLover.find(:first).pies.count

But this fails:  
PieLover.find(:first).cherry\_pies.count

I tried adding “belongs\_to :pie\_lover” in cherry\_pie.rb but it didn’t  
make a difference. I’m guessing that I’m missing something simple.

Thanks for the guidance, folks!

- Don

---

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [October 12, 2006, 5:16am UTC](https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891/2 "2006-10-12T05:16:32Z")

</div>

On Oct 11, 10:48 pm, Don S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class Pie \< ActiveRecord::Base
> 
> I tried adding “belongs\_to :pie\_lover” in cherry\_pie.rb but it didn’t  
> make a difference. I’m guessing that I’m missing something simple.
> 
> Thanks for the guidance, folks!
> 
> - Don
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

If you want to find all pies, then  
‘PieLover.find(:first).pies.count’

If you want just CherryPies, then PieLover has to ‘has\_many  
:cherry\_pies’.

\_Kevin

---

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [October 12, 2006, 5:33am UTC](https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891/3 "2006-10-12T05:33:27Z")

</div>

> I tried adding “belongs\_to :pie\_lover” in cherry\_pie.rb but it didn’t  
> make a difference. I’m guessing that I’m missing something simple.

OK, it was something simple. I didn’t add “has\_many :cherry\_pies” to  
pie\_lover.rb. Duh!

Since the STI section of the Agile Rails book doesn’t mention STI and  
relationships, I assumed that you could get away with declaring the  
relationship at the base of the object model. I didn’t realize that I  
needed to declare each subclass explicitly. But since that’s how  
everything else works… guess it’s time for a break!

Thanks!

---

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [October 12, 2006, 5:35am UTC](https://www.ruby-forum.com/t/sti-troubles-in-a-has-many-belongs-to-relationship/71891/4 "2006-10-12T05:35:33Z")

</div>

> If you want just CherryPies, then PieLover has to ‘has\_many  
> :cherry\_pies’.

Thanks Kevin! I did figured it out as you were typing. 😉

Thanks!
