Find file
Fetching contributors…
Cannot retrieve contributors at this time
89 lines (62 sloc) 2.18 KB
If you are using Rails 2.3.8 or other version < 3, you have to use the version 1.1.x of this plugin, for Rails 3 you need to use the version 1.2.x or master of this plugin.
ClassTableInheritance 1.2.1
This is an ActiveRecord plugin designed to allow
simple multiple table (class) inheritance.
This plugin was inspired by:
inherits_from plugin => and
Multiple Table Inheritance with ActiveRecord =>
How to install
gem install class-table-inheritance
# Migrations
create_table :products do |t|
t.string :description, :null => false
t.string :subtype # Only if you need access of both side see example
t.decimal :price
create_table :books, :inherits => :product do |t|
t.string :author, :null => false
create_table :videos, :inherits => :product do |t|
t.string :year, :null => false
t.string :genre, :null => false
# Models
class Product < ActiveRecord::Base
acts_as_superclass # only if you want top-down access.
class Book < ActiveRecord::Base
inherits_from :product
class Video < ActiveRecord::Base
inherits_from :product
book = Book.find(1) => "Agile Development with Rails" => "Dave Thomas"
book.price => 19.00
video = Video.find(2) => "Inseption"
video.year => "2010"
video.genre => "SCI-FI"
video.price => 22.00
book = = "Hamlet" = "Shakespeare, William"
book.price => 14.00
Top-down access (Polymorphic)
if you want to access product and get field in the subclass do you need to create a field subtype:string in superclass and ad acts_as_superclass in superclass and now you can do like this.
product = Product.find 1 # This is a Book instance.
product = Product.find 2 # This is a Video instance.
if you need help contanct me: bfscordeiro (at) .
Copyright (c) 2010 Bruno Cordeiro, released under the MIT license