RSS RSS feed | Atom Atom feed
Popular Articles: Tom Riddle's Magical Diary | AJAX Lego Robot | AJAX CAPTCHA | SQL Multisets

Single Table Polymorphic Inheritance with ActiveRecords

Ruby on Rails warning: toplevel constant XX referenced by YY

Implementing a single table polymorphic inheritance in rails is dead easy. Simply do
class Fruit < ActiveRecord::Migration def self.up create_table :persons do |t| t.column :type :string t.column :shape, :string t.timestamps end end def self.down drop_table :fruits end end
or if you a migrating...
class Fruit < ActiveRecord::Migration def self.up add_column :fruits, :type, :string end def self.down remove_column :fruits, :type end end
Now, all you have to do is to create your sub-classes
class Fruit < ActiveRecord::Base def calc_volume ... end end class Apple < Fruit def calc_volume ... end end
Don't forget to put Apple in a separate file by itself apple.rb or else ruby will spit out an unconditional warning if you reference Apple in your code with Fruit::Apple
warning: toplevel constant Fruit referenced by Fruit::Apple

Read more...

Tags :
slashdot digg del.icio.us technorati [more]




Add a comment Send a TrackBack