# Single Table Inheritance (this is my 3rd post :( )

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361>\
**Category:** Rails\
**Created:** [January 13, 2006, 4:52am UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361 "2006-01-13T04:52:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikrant\_Rathore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/958977/32.png) [@Vikrant\_Rathore](https://www.ruby-forum.com/u/Vikrant_Rathore)\
**Post date:** [January 13, 2006, 4:52am UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361/1 "2006-01-13T04:52:19Z")

</div>

Hi Everyone,

I hope I get some feedback on my question. Here it goes.

I have a situation here, I have a company table and then using Single  
Table inheritance it is of 3 types:

class Company \< ActiveRecord::Base  
end

class Vendor \< Company  
end

class Customer \< Company  
end

class Agent \< Company  
end

Now I have a active record Association

class QuoteSheet \< ActiveRecord::Base  
belongs\_to :customer  
has\_many :vendors  
has\_one :Agent  
end

Now in the table quote\_sheets should I use vendor\_id, customer\_id,  
agent\_id or just company\_id since all of them are in 1 table called  
companies.

I have read agile wweb development with rail, and no where it says about  
relationship like this.

Thanks  
with best regards,  
Vikrant

---

<div class="post-metadata">

**Author:** ![Vikrant\_Rathore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/958977/32.png) [@Vikrant\_Rathore](https://www.ruby-forum.com/u/Vikrant_Rathore)\
**Post date:** [January 13, 2006, 5:46am UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361/2 "2006-01-13T05:46:52Z")

</div>

Vikrant Rathore wrote:

> class QuoteSheet \< ActiveRecord::Base  
> belongs\_to :customer  
> has\_many :vendors  
> has\_one :Agent  
> end
> 
> Now in the table quote\_sheets should I use vendor\_id, customer\_id,  
> agent\_id or just company\_id since all of them are in 1 table called  
> companies.

Yeah, it is a bit unclear, but this is what I would try…

class Quotesheet  
belongs\_to :customer, :foreign\_key=\>‘customer\_id’

…

class Customer  
has\_many :quote\_sheets, :foreign\_key +\> ‘customer\_id’  
…

Since you are using belongs\_to/has\_many I assume you don’t really want a  
HABTM, which is the only reason you would need the ‘quote\_sheets’ table.

\_Kevin

---

<div class="post-metadata">

**Author:** ![Vikrant\_Rathore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/958977/32.png) [@Vikrant\_Rathore](https://www.ruby-forum.com/u/Vikrant_Rathore)\
**Post date:** [January 13, 2006, 8:17am UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361/3 "2006-01-13T08:17:58Z")

</div>

Vikrant Rathore wrote:

> class Vendor \< Company  
> class QuoteSheet \< ActiveRecord::Base  
> belongs\_to :customer  
> has\_many :vendors  
> has\_one :Agent  
> end

Without knowing all the details I can’t be sure, but my first impression  
is that vendor, customer and agent are roles rather than types. Could  
the same Company be a vendor with respect to one QuoteSheet, and a  
customer with respect to another? If you use subclasses in the way you  
suggest, this wouldn’t be possible.

I think your associations need a little attention:

belongs\_to :customer looks fine - so a customer would be able to have  
many QuoteSheets

has\_many :vendors looks wrong - that would mean a vendor would belong to  
one QuoteSheet - I think it should be has\_and\_belongs\_to\_many

has\_one :agent (not :Agent) looks wrong - that would mean an agent would  
belong to one QuoteSheet - it should be belongs\_to (which really means  
refers\_to)

QuoteSheet might look something like:

class QuoteSheet \< ActiveRecord::Base

belongs\_to :customer,  
:class\_name =\> “Company”,  
:foreign\_key =\> “customer\_id”

belongs\_to :agent,  
:class\_name =\> “Company”,  
:foreign\_key =\> “agent\_id”

has\_and\_belongs\_to\_many :vendors,  
:class\_name =\> “Company”,  
:join\_table =\> “quote\_sheets\_vendors”

end

(absolutely untested!)

You would have to think about how to name the other ends of the  
associations, i.e. the collection of QuoteSheets received as a customer,  
the collection of QuoteSheets issued as an agent, and the collection of  
QuoteSheets quoted on as a vendor.

Is there a bit of model missing here - the actual quote from each  
vendor?

HTH

Justin

---

<div class="post-metadata">

**Author:** ![Vikrant\_Rathore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/958977/32.png) [@Vikrant\_Rathore](https://www.ruby-forum.com/u/Vikrant_Rathore)\
**Post date:** [January 14, 2006, 11:40am UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361/4 "2006-01-14T11:40:20Z")

</div>

Mailing L. wrote:

> at the single inheritance problem. But it seems your suggested method  
> might work in which i specify the foreign key.

In case it wasn’t clear, I was suggesting that you shouldn’t use  
inheritance - just use Company in multiple roles.

Good luck!

Justin

---

<div class="post-metadata">

**Author:** ![Vikrant\_Rathore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/958977/32.png) [@Vikrant\_Rathore](https://www.ruby-forum.com/u/Vikrant_Rathore)\
**Post date:** [January 13, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-this-is-my-3rd-post/52361/5 "2006-01-13T17:25:27Z")

</div>

Hi Justin and Kevin,

Thanks for your response. This is just an illustration of the problem  
indeed the actual DB design includes over 50 tables and different since  
this application is for a large Supply Chain Management (in  
commodities). Currently we have an .net based system with 150 tables and  
another 2 systems based on J2EE with over 200 tables. I was able to  
squeeze all these into little over 50 tables.

Now I want to build a solution on one top of the other. But then stuck  
at the single inheritance problem. But it seems your suggested method  
might work in which i specify the foreign key.

Thanks  
with best regards  
Vikrant
