I just implemented very simple and primitive class table inheritance in ActiveRecord. The goal is for this to become mature enough to become a true feature in Rails. Check it out here:
http://guest@blog.raylucke.com/svn/inherits_from
Here’s how it looks in action:
create_table "books", :force => true do |t|
t.column "product_id", :integer
t.column "pages", :integer
t.column "author", :string
end
create_table "products", :force => true do |t|
t.column "name", :string
t.column "price", :integer
t.column "subtype", :string
end
create_table "videos", :force => true do |t|
t.column "product_id", :integer
t.column "minutes", :integer
t.column "starring", :string
end
class Product < ActiveRecord::Base
end
class Book < ActiveRecord::Base
inherits_from :product
end
class Video < ActiveRecord::Base
inherits_from :product
end
Book.create(:name => "Agile Development with Rails", :pages => 350)
Video.create(:name => "Twilight Zone Season 1", :minutes => 490)
Book.find(1).name => "Agile Development with Rails"
Note: this plugin is not production ready. I’m hoping some people who are much smarter than me will send me patches to make it primetime.
Known bugs:
- Inherited column validation/error handling does not currently work
- Book.find_by_name() (inherited column) does not work
- Only basic attributes are passed along right now
Feel free to send me any comments to ray @ this domain (raylucke.com).
Updated 4/24/07: Thanks Eric for the patches!