user1002119
user1002119

Reputation: 3812

How do I implement class-table-inheritance in Rails?

I just finished working my way through the book Agile Web Development with Rails, fourth edition (http://pragprog.com/book/rails4/agile-web-development-with-rails), and need some help in understanding how to create a parent-child relationship between models. I did some Googling and poked around here on StackOverflow and learned generally about single table inheritance and class table inheritance.

I have a bunch of metadata about different items in an archival collection. Much of the metadata is the same for all objects, e.g., accession number, title, description, copyright, etc. In addition to the common metadata, there is metadata that is only applicable to certain types of items, e.g., photos need to keep track of whether the item is in color or black and white; sound recordings need to keep track of the format (CD, DAT, cassette tape, etc.) and duration; text materials need to keep track of the number of pages; etc.

       Class hierarchy

            Item
              |
  +-----------+------------+
  |           |            |
Photo   SoundRecording    Text

Since I will have many thousands of records I have ruled out single table inheritance as being too inefficient. I understand the idea of class table inheritance as presented here: http://martinfowler.com/eaaCatalog/classTableInheritance.html but am confused on how I would actually implement this in a new Ruby on Rails project. Do I implement class table inheritance as a polymorphic association (http://guides.rubyonrails.org/association_basics.html#polymorphic-associations) or am I on the wrong track?

Thank you.

Upvotes: 14

Views: 3997

Answers (3)

Tom Rossi
Tom Rossi

Reputation: 12086

Delegated Types is very similar to class table inheritance.

Upvotes: 0

Meysam Feghhi
Meysam Feghhi

Reputation: 945

There is a gem that does this cleanly and it also supports rails 5.1

https://github.com/mvdamme/dbview_cti

Upvotes: 0

EasierSaidThanDone
EasierSaidThanDone

Reputation: 1897

Yes. It boils down to this (in Rails 3)

class Business < ActiveRecord::Base
  belongs_to  :biz, :polymorphic => true
end

class Restaurant < ActiveRecord::Base
  acts_as_biz
end

Here's a very good tutorial on how to do it.

http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/

Upvotes: 6

Related Questions