David Vallner
12/28/2006 12:47:00 AM
Ryan Glover wrote:
> Hello,
>
> I am developing an application that will have 100's of model classes
> each derived from a single source class. Each model will have 4 similar
> attributes but then have about a dozen unique ones each.
Why are they deriving from a single class anyway?
If it's only for a few identical attributes without significant
functionality to go along, I'd say it's a bad idea to use inheritance.
And with latent typing in Ruby, you Rarely If Ever need inheritance
anyway. Saves you the bother that is inheritance mapping at any rate.
> Problem is, I'll end up with 100's of
> tables, which also does not appeal to me.
>
I can't understand this. It's rather common that an ORM database schema
has a number of tables in the same order of magnitude as there are model
classes. My biased opinion is that data should be modelled on the
database level where you have some theorethical foundations about what
are "good shapes" for the data. It also doesn't hurt to at least make
the DB schema somewhat document the data model - the data an application
gathers / stores is the most likely part of it to persist in the long
term. Of course, you're probably in a better position to judge this.
> My third solution, the one I am asking advice on, is this.
>
> I create a single table with the 4 common columns and a dozen columns
> with names such as float1, float2, float3, ... , string1, string2, ..
> etc. Creating enough columns of each type to cover my largest subclass.
> Then I use STI and for each class I map the generic columns to the nicer
> names inside the subclasses. So in one class float1 may be miles/hour
> while in another class it might be turkeys/hectare.
>
> Does this sound like a reasonable approach?
>
Database schemas like this tend to make it into The Daily WTF with some
regularity.
(Unfortunately, they also make it into production systems.)
If there's a chance someday, someone, somewhy might want to access your
data at a DB level (using a reporting tool maybe), there'd be no end to
the grief.
Or, if a bug in your code gets the database into an inconsistent state,
and you have to drop down to SQL to patch things up.
Avoid.
> And of course, does anyone see a much better way to do what I have
> proposed?
>
Use migrations to create the schema from scratch if you just want to
avoid writing a lot of SQL up front?
David Vallner