14

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.

4

2 回答 2

6

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/

于 2012-07-04T09:32:08.990 回答
0

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

https://github.com/mvdamme/dbview_cti

于 2017-08-18T00:11:17.203 回答