# What's the best way to do complex inheritance?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578>\
**Category:** Rails\
**Created:** [January 3, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578 "2006-01-03T20:37:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [January 3, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578/1 "2006-01-03T20:37:19Z")

</div>

I have a Merchant class that has many Products. Product is an abstract  
class which has many subclasses (ProductA, ProductB, etc). All Products  
have common data, like ‘name’, ‘status’, ‘created\_on’, etc… but they  
also have some completely different properties.

It seems like Active Record only supports Single Table Inheritance.  
This would make my Products’ table _huge_ and mostly empty. Is there a  
way around this or am I stuck with a sloppy table? Would I be better of  
just having seperate lists for each Product that the merchant has?

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [January 3, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578/2 "2006-01-03T21:00:22Z")

</div>

Stephen,  
Single Table Inheritance will work in this case if you pull out the  
extraneous attributes and put them in auxillary tables. Then you can  
associate the appropriate attiribute tables to subclasses of Product as  
needed.  
JM2C  
Matt

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [January 3, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578/3 "2006-01-03T21:06:33Z")

</div>

On 1/3/06, Stephen G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Product.has\_many :attributes

create\_table(:attributes) do |t|  
t.column :key, :string  
t.column :value, :text  
end

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [January 3, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-complex-inheritance/51578/4 "2006-01-03T21:38:59Z")

</div>

Matt Goss wrote:

> Stephen,  
> Single Table Inheritance will work in this case if you pull out the  
> extraneous attributes and put them in auxillary tables. Then you can  
> associate the appropriate attiribute tables to subclasses of Product as  
> needed.  
> JM2C  
> Matt

I tried something like this. I made a ProductAData and made it belong  
to ProductA. I then created a view so that I could create a new  
ProductA with its ProductAData along with it. If the ProductA fields  
are valid, but the ProductAData isn’t, the new ProductA is saved to the  
database without the ProductAData. What’s the proper way to do this?

BTW, I’m a little green with this stuff (if it wasn’t obvious).
