Forum: Ruby on Rails Class Table Inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rick M. (Guest)
on 2007-02-23 22:25
(Received via mailing list)
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?
Luke I. (Guest)
on 2007-02-24 00:20
(Received via mailing list)
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.
Sava C. (Guest)
on 2007-03-05 19:23
(Received via mailing list)
Rick M. wrote:
> class Movie < Product; end;
>  - updated_on
> movie attributes:
>  - 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/

--
Sava C.
ClTI plugin author
This topic is locked and can not be replied to.