# Inheritance with ORM (ActiveRecord)

**URL:** <https://www.ruby-forum.com/t/inheritance-with-orm-activerecord/123942>\
**Category:** Ruby\
**Created:** [December 22, 2007, 11:32am UTC](https://www.ruby-forum.com/t/inheritance-with-orm-activerecord/123942 "2007-12-22T11:32:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 22, 2007, 11:32am UTC](https://www.ruby-forum.com/t/inheritance-with-orm-activerecord/123942/1 "2007-12-22T11:32:40Z")

</div>

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
