[Rails] SingleTableInheritance Considered Harmful

Aled Davies awd at well.com
Fri Nov 19 14:20:56 GMT 2004


This one bit me as well when I started using Rails, and while using a 
mixin module works, it always looks a bit out of place in the otherwise 
fairly clean model code, and somehow doesn't feel quite right. That 
said I like the single table inheritance idea as it seems to work 
fairly well and is a fairly powerful and unique feature.

What would be a wish for me is if the framework created an 
'abstract_model' class (that extends ActiveRecord::Base) in the model 
directory. All models for that webapp would extend 'abstract_model' 
(similar to the way all controllers extend 'abstract_controller'), and 
would provide a place to for people to put common functionality that 
all the subclasses can inherit without having to go to the mixin route. 
Of course the class should be invisible to the single table inheritance 
model and so that first level subclasses don't try and write a type 
column when persisted.

Just my 2 cents there.

	A.

--
Aled Davies <awd at well.com>

> Curt & DHH,
>
> I 100% agree. Single Table Inheritance is a really bad code/db smell.
>
> Not only does it break the rules of good database normalization, it
> causes class inheritence to behave in unexpected, "magic" ways.
>
>
> On Fri, 19 Nov 2004 15:39:13 +0900 (JST), Curt Sampson <cjs at cynic.net> 
> wrote:
>>
>> Here's the text of a quick note I've added to the Inheritance wiki 
>> page.
>> I'm actually proposing that it would be a good idea to drop automatic
>> single table inheritance and force people to declare it explicitly.
>> There are two reasons for this:
>>
>>     1. Implicitly doing it unnecessarially limits your use of
>>     inheritance where you really need it: in your object-oriented
>>     programming language. (This is what nailed me--I was doing a 
>> simple
>>     pull-up-method refactoring and suddenly it started complaining 
>> that
>>     I had no 'type' column in my table.)
>>
>>     2. It's entirely non-relational, and thus bad practice to suggest
>>     to those who do not well understand relational DBMSes (which is 
>> the
>>     vast majority of programmers out there) that this is any sort of a
>>     valid technique. SingleTableInheritance is a tool of desperation,
>>     when you can't change your database schema to be relationally 
>> sound
>>     and make sense.
>>
>> It would in fact be nice if ActiveRecord provided support for class
>> table inheritance.
>
> -- 
> Regards,
> John Wilger
>
> -----------
> Alice came to a fork in the road. "Which road do I take?" she asked.
> "Where do you want to go?" responded the Cheshire cat.
> "I don't know," Alice answered.
> "Then," said the cat, "it doesn't matter."
> - Lewis Carrol, Alice in Wonderland
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2353 bytes
Desc: not available
Url : http://one.textdrive.com/pipermail/rails/attachments/20041119/7977202b/smime.bin


More information about the Rails mailing list