From: Thufir Date: 2007-12-22T19:32:02+09:00 Subject: inheritance with ORM (ActiveRecord) Maybe this is for the rails group since it involves ActiveRecord? In any event, I have a db with a single table holding some Creature objects. When the Creature objects are instantiated, they're actually Dragons (which inherit from Creature, of course). How is their inheritance reflected in the db? thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ ruby creatures.rb -- create_table(:creatures) SQL (0.011286) CREATE TABLE creatures ("id" INTEGER PRIMARY KEY NOT NULL, "life" integer DEFAULT NULL, "strength" integer DEFAULT NULL, "charisma" integer DEFAULT NULL, "weapon" integer DEFAULT NULL) -> 0.1332s #0, "charisma"=>0, "weapon"=>0, "life"=>0}> SQL (0.000884) INSERT INTO creatures ("strength", "charisma", "life", "weapon") VALUES(0, 0, 0, 0) #0, "charisma"=>0, "weapon"=>0, "life"=>0}> SQL (0.000948) INSERT INTO creatures ("strength", "charisma", "life", "weapon") VALUES(0, 0, 0, 0) #0, "charisma"=>0, "weapon"=>0, "life"=>0}> SQL (0.000686) INSERT INTO creatures ("strength", "charisma", "life", "weapon") VALUES(0, 0, 0, 0) #0, "charisma"=>0, "weapon"=>0, "life"=>0}> SQL (0.000703) INSERT INTO creatures ("strength", "charisma", "life", "weapon") VALUES(0, 0, 0, 0) #0, "charisma"=>0, "weapon"=>0, "life"=>0}> SQL (0.000700) INSERT INTO creatures ("strength", "charisma", "life", "weapon") VALUES(0, 0, 0, 0) thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat creatures.rb require 'fileutils' require 'active_record' require 'Creature' require 'Dragon' system("rm dwemthys.db") ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = true ActiveRecord::Base.establish_connection( :adapter => "sqlite3", :dbfile => "dwemthys.db" ) ActiveRecord::Schema.define do create_table :creatures do |table| table.column :life, :integer table.column :strength, :integer table.column :charisma, :integer table.column :weapon, :integer end end 5.times { creature = Dragon.new p creature creature.save } thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat Creature.rb require 'active_record' class Creature < ActiveRecord::Base attr_accessor :life, :strength, :charisma, :weapon def to_s () print "class\t\t" print self.class print "\nlife\t\t" print @life print "\nstrength\t" print @strength print "\ncharisma\t" print @charisma print "\nweapon\t\t" print @weapon print "\n" end end thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat Dragon.rb require 'Creature' class Dragon < Creature end thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thanks, Thufir