I'm trying to find a solution to this modeling problem I'm having in
Rails, mainly because of the lack of support for ClassTI in Rails.
I'm building a highly complex CMS which will be handling different
types of content groups (objects) which each have their own unique set
of attributes. For example:
class Product < ActiveRecord::Base; end
class Game < Product; end;
class Movie < Product; end;
class Car < Product; end;
Normally this can be handled fine by STI but each model has a very
unique set of attributes.
product attributes:
- id
- title
- description
- created_on
- updated_on
game attributes:
- id
- title [inherited]
- description [inherited]
- esrb_rating_id
- game_platform_id
- created_on [inherited]
- updated_on [inherited]
movie attributes:
- id
- title [inherited]
- description [inherited]
- mpaa_rating_id
- production_studio_id
- created_on [inherited]
- updated_on [inherited]
car attributes:
- id
- title [inherited]
- description [inherited]
- mpg_city
- mpg_highway
- created_on [inherited]
- updated_on [inherited]
Can anyone suggest a good solution for me that will allow me to still
keep a DRY environment?
The first thing that pops into my head as I’m getting ready to walk out the door is:
Create an extra table for each of those sub-types that will use a has_one, belongs_to type of relationship with the STI table, keeping only the extra attributes that are specialized in the extra tables.
In the end, you’d end up with something like
c = Car.find(id)
c.title == ‘Blah’
c.car_attributes.mpg_city == 24
It seems a little less than perfect, but it’s the only thing I can think of in 3 minutes that will work.
I'm trying to find a solution to this modeling problem I'm having in
Rails, mainly because of the lack of support for ClassTI in Rails.
I'm building a highly complex CMS which will be handling different
types of content groups (objects) which each have their own unique set
of attributes. For example:
class Product < ActiveRecord::Base; end
class Game < Product; end;
class Movie < Product; end;
class Car < Product; end;
Normally this can be handled fine by STI but each model has a very
unique set of attributes.
product attributes:
- id
- title
- description
- created_on
- updated_on
game attributes:
- id
- title [inherited]
- description [inherited]
- esrb_rating_id
- game_platform_id
- created_on [inherited]
- updated_on [inherited]
movie attributes:
- id
- title [inherited]
- description [inherited]
- mpaa_rating_id
- production_studio_id
- created_on [inherited]
- updated_on [inherited]
car attributes:
- id
- title [inherited]
- description [inherited]
- mpg_city
- mpg_highway
- created_on [inherited]
- updated_on [inherited]
Can anyone suggest a good solution for me that will allow me to still
keep a DRY environment?
Yes. Switch to PostgreSQL and use Class Table Inheritance plugin from svn://rubyforge.org/var/svn/clti/