inheritance with ORM (ActiveRecord)

T

Thufir

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
#<Dragon:0xb773f818 @new_record=true, @attributes={"strength"=>0,
"charisma"=>0, "weapon"=>0, "life"=>0}>
SQL (0.000884) INSERT INTO creatures ("strength", "charisma", "life",
"weapon") VALUES(0, 0, 0, 0)
#<Dragon:0xb773735c @new_record=true, @attributes={"strength"=>0,
"charisma"=>0, "weapon"=>0, "life"=>0}>
SQL (0.000948) INSERT INTO creatures ("strength", "charisma", "life",
"weapon") VALUES(0, 0, 0, 0)
#<Dragon:0xb7734184 @new_record=true, @attributes={"strength"=>0,
"charisma"=>0, "weapon"=>0, "life"=>0}>
SQL (0.000686) INSERT INTO creatures ("strength", "charisma", "life",
"weapon") VALUES(0, 0, 0, 0)
#<Dragon:0xb7732780 @new_record=true, @attributes={"strength"=>0,
"charisma"=>0, "weapon"=>0, "life"=>0}>
SQL (0.000703) INSERT INTO creatures ("strength", "charisma", "life",
"weapon") VALUES(0, 0, 0, 0)
#<Dragon:0xb7730318 @new_record=true, @attributes={"strength"=>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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top