# Generalisation in RoR

**URL:** <https://www.ruby-forum.com/t/generalisation-in-ror/207266>\
**Category:** Rails\
**Created:** [May 22, 2011, 10:39am UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266 "2011-05-22T10:39:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 22, 2011, 10:39am UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/1 "2011-05-22T10:39:06Z")

</div>

Hello G.s,

does anybody know wheter RoR is able to implement a generalisation of  
a database? I didn’t find any solution for that on the internet.

Example

Person : Customer

or

Person : Employee

in the database such a generalisation is characterized by having the  
same PK in Customer and Person or Employee and Person!

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 22, 2011, 10:43am UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/2 "2011-05-22T10:43:25Z")

</div>

On 22 May 2011 09:37, Wolfgang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> or
> 
> Person : Employee
> 
> in the database such a generalisation is characterized by having the  
> same PK in Customer and Person or Employee and Person!

Would Single Table Inheritance (STI) do what you want?

Colin

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 22, 2011, 12:37pm UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/3 "2011-05-22T12:37:07Z")

</div>

STI is a good approach, but my “subtables” have additional attributes  
and afaik this isn’t supportet by STI, is it?

For example:

user \<\< Base  
id  
firstname  
lastname  
address  
end

employee \<\< user  
  
salary  
end

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 23, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/4 "2011-05-23T16:19:34Z")

</div>

Wolfgang wrote in post #1000187:

> STI is a good approach, but my “subtables” have additional attributes  
> and afaik this isn’t supportet by STI, is it?
> 
> For example:
> 
> user \<\< Base  
> id  
> firstname  
> lastname  
> address  
> end
> 
> employee \<\< user  
>   
> salary  
> end

In the past I used a framework that supported other techniques besides  
Single-Table Inheritance. There were actually two other options that  
were called Horizontal and Vertical Inheritance. Of those options  
Vertical Inheritance was the closest to what we think of as object  
inheritance (as you described above), but was also the least efficient.

The issue is that both Horizontal and Vertical inheritance require  
multiple queries to get to all the data you need, which causes  
performance problems. Even with a framework that supported the other  
techniques, using STI was strongly recommended. The performance costs of  
either Horizontal or Vertical was simply too high. Inheritance doesn’t  
map well to RDBMS. The cost of the null fields in a single table that  
contains all attributes for the entire inheritance hierarchy is  
negligible compared to the other techniques.

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 22, 2011, 12:40pm UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/5 "2011-05-22T12:40:50Z")

</div>

On 22 May 2011 11:35, Wolfgang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> STI is a good approach, but my “subtables” have additional attributes  
> and afaik this isn’t supportet by STI, is it?

Yes, you just put the whole set of all attributes in the table. It  
wastes a small amount of space in the db but generally that is not an  
issue.

Colin

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 23, 2011, 4:30pm UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/6 "2011-05-23T16:30:02Z")

</div>

Honestly, I’ve always preferred single-table inheritance. I am new to  
Rails, but in Java, this is what I’ve always done and it just works  
really nicely and it’s more performant than forcing multiple joins  
whenever you want to query users - which is probably going to be  
often.

I am finding other problems that are Rails-specific with subclasses…  
like when submitting forms with simple\_for, the parameters hash isn’t  
the base class by default, so I am having trouble forcing it to be the  
base-class… but structurally, single-table inheritance is a sound  
practice. Been doing it for over a decade.

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [May 23, 2011, 7:47pm UTC](https://www.ruby-forum.com/t/generalisation-in-ror/207266/7 "2011-05-23T19:47:58Z")

</div>

Thank you very much for your answers, I’ve decided to use STI
