Generalisation in RoR

Hello Guys,

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!

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

Colin

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 <all of the above and additionally:>   salary end

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

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 <all of the above and additionally:>   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.

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.

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