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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With