# Class Table Inheritance

**URL:** <https://www.ruby-forum.com/t/class-table-inheritance/86624>\
**Category:** Rails\
**Created:** [February 23, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/class-table-inheritance/86624 "2007-02-23T21:25:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/90ced4/32.png) [@rickm](https://www.ruby-forum.com/u/rickm)\
**Post date:** [February 23, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/class-table-inheritance/86624/1 "2007-02-23T21:25:53Z")

</div>

I’m trying to find a solution to this modeling problem I’m having in  
Rails, mainly because of the lack of support for ClassTI in Rails.

I’m building a highly complex CMS which will be handling different  
types of content groups (objects) which each have their own unique set  
of attributes. For example:

class Product \< ActiveRecord::Base; end

class Game \< Product; end;  
class Movie \< Product; end;  
class Car \< Product; end;

Normally this can be handled fine by STI but each model has a very  
unique set of attributes.

product attributes:

- id
- title
- description
- created\_on
- updated\_on

game attributes:

- id
- title [inherited]
- description [inherited]
- esrb\_rating\_id
- game\_platform\_id
- created\_on [inherited]
- updated\_on [inherited]

movie attributes:

- id
- title [inherited]
- description [inherited]
- mpaa\_rating\_id
- production\_studio\_id
- created\_on [inherited]
- updated\_on [inherited]

car attributes:

- id
- title [inherited]
- description [inherited]
- mpg\_city
- mpg\_highway
- created\_on [inherited]
- updated\_on [inherited]

Can anyone suggest a good solution for me that will allow me to still  
keep a DRY environment?

---

<div class="post-metadata">

**Author:** ![rickm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/90ced4/32.png) [@rickm](https://www.ruby-forum.com/u/rickm)\
**Post date:** [February 23, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/class-table-inheritance/86624/2 "2007-02-23T23:20:13Z")

</div>

The first thing that pops into my head as I’m getting ready to walk out  
the  
door is:

Create an extra table for each of those sub-types that will use a  
has\_one,  
belongs\_to type of relationship with the STI table, keeping only the  
extra  
attributes that are specialized in the extra tables.

In the end, you’d end up with something like  
c = Car.find(id)  
c.title == ‘Blah’  
c.car\_attributes.mpg\_city == 24

It seems a little less than perfect, but it’s the only thing I can think  
of  
in 3 minutes that will work.

---

<div class="post-metadata">

**Author:** ![rickm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/90ced4/32.png) [@rickm](https://www.ruby-forum.com/u/rickm)\
**Post date:** [March 5, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/class-table-inheritance/86624/3 "2007-03-05T18:23:20Z")

</div>

Rick M. wrote:

> class Movie \< Product; end;
> 
> - updated\_on  
> movie attributes:
> - title [inherited]
> - description [inherited]
> - mpg\_city
> - mpg\_highway
> - created\_on [inherited]
> - updated\_on [inherited]
> 
> Can anyone suggest a good solution for me that will allow me to still  
> keep a DRY environment?

Yes. Switch to PostgreSQL and use Class Table Inheritance plugin from  
svn://rubyforge.org/var/svn/clti/

–  
Sava C.  
ClTI plugin author
