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?


[email protected] ~/Desktop/dwemthys $
[email protected] ~/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)
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
}
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ cat Dragon.rb
require 'Creature'

class Dragon < Creature


end
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $


thanks,

Thufir
 
Ad

Advertisements


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

Top