# Model inheritence and relational databases

**URL:** <https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858>\
**Category:** Rails\
**Created:** [April 2, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858 "2007-04-02T13:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [April 2, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858/1 "2007-04-02T13:26:37Z")

</div>

Okay the problem I am trying to get to grips with is how inheritence and  
data storage in a relational database can work…

I can easily have a model inherit from other models and if the  
difference between the two models is fairly small STI works well… But  
once you start adding more and more functionality (well properties  
actually) STI becomes messy and you want to split the single table up in  
to multiple table keeping one table per model…

What is the best way to tackle this?

Lets say I have a base model called Animal which is supposed to have the  
properties, weight, age and health. I then create two new models called  
Bird and Insect. Bird has the property flying and Insect has the  
property bugging…

Would I create two tables, with the full set of fields in, or is there a  
way in Rails to keep the common fields in one table and the additional  
fields in another table? (I guess not)

BirdTBL  
id, weight, age, health, flying

InsectTBL  
id, weight, age, health, bugging

[For the sake of this example please imagine there are loads of other  
animals each that extends the Animals class, so STI would not be  
useful…]

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [April 11, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858/2 "2007-04-11T15:29:58Z")

</div>

Kris wrote:

> Okay the problem I am trying to get to grips with is how inheritence and  
> data storage in a relational database can work…
> 
> I can easily have a model inherit from other models and if the  
> difference between the two models is fairly small STI works well… But  
> once you start adding more and more functionality (well properties  
> actually) STI becomes messy and you want to split the single table up in  
> to multiple table keeping one table per model…
> 
> What is the best way to tackle this?

Take a look at Class table inheritance  
[P of EAA: Class Table Inheritance](http://www.martinfowler.com/eaaCatalog/classTableInheritance.html). If  
this  
concept suits you, you can give class table inheritance plugin a try at  
[http://rubyforge.org/projects/clti/](http://rubyforge.org/projects/clti/). Disclaimer: I’m its author.

–  
Sava C.

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [April 16, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858/3 "2007-04-16T12:48:54Z")

</div>

Sava C. wrote:

> Kris wrote:
> 
> > What is the best way to tackle this?
> 
> Take a look at Class table inheritance  
> [P of EAA: Class Table Inheritance](http://www.martinfowler.com/eaaCatalog/classTableInheritance.html). If  
> this  
> concept suits you, you can give class table inheritance plugin a try at  
> [http://rubyforge.org/projects/clti/](http://rubyforge.org/projects/clti/). Disclaimer: I’m its author.
> 
> –  
> Sava C.

Thanks Sava, thats the word I needed, CTI. Ive had a good google and I  
have found quite a few discussion and implementations but nothing stable  
or that does not remove certain ActiveRecord features…

I guess its either put up or contribute code 🙂

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [April 17, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/model-inheritence-and-relational-databases/90858/4 "2007-04-17T16:37:34Z")

</div>

Guest wrote:

> > –  
> > Sava C.
> 
> Thanks Sava, thats the word I needed, CTI. Ive had a good google and I  
> have found quite a few discussion and implementations but nothing stable  
> or that does not remove certain ActiveRecord features…

My implementation hopefully does not remove any ActiveRecord feature. It  
has  
some unit tests, but test coverage is far from complete - I’d like to  
add all  
inheritance tests from ActiveRecord. The plugin also has not been  
performance  
tested.

–  
Sava C.
