# STI and HABTM

**URL:** <https://www.ruby-forum.com/t/sti-and-habtm/94502>\
**Category:** Rails\
**Created:** [May 5, 2006, 6:34am UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502 "2006-05-05T06:34:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 6:34am UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502/1 "2006-05-05T06:34:07Z")

</div>

Hey gang,

How does STI work with HABTM?

## For instance…let’s say I have the following models

Category \< ActiveRecord::Base

## Item \< ActiveRecord::Base Product \< Item Deal \< Item

Each product or deal can have categories. Would I then setup Item with  
has\_and\_belongs\_to\_many :categories, then create a table  
categories\_items  
??? Is there a better way to do it?

Haven’t done this with STI yet - Thanks.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 7:04am UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502/2 "2006-05-05T07:04:31Z")

</div>

Moreover, why can’t I do a Deal.find or a Product.find ???

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 9:45am UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502/3 "2006-05-05T09:45:32Z")

</div>

You should be able to do a Deal.find. I have a very similar setup and  
have no prob. Wondering if it’s finding the subclass declarations. Are  
the subclasses in the same file: item.rb or in their own ones? Yes,  
you’d have the has\_and\_belongs\_to\_many :categories declaration in the  
item setup, and the subclasses would inherit the relationship.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 9:57am UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502/4 "2006-05-05T09:57:57Z")

</div>

They are all in their own separate files - do they need to be all in one  
file???

If I try Product.find or Deal.find from script/console I get a SQL error  
telling me the table products or deals doesn’t exist.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/sti-and-habtm/94502/5 "2006-05-05T19:22:58Z")

</div>

I’m an idiot - I had Product still subclassing ActiveRecord, not Item…

THanks 🙂
