# Single Table Inheritance implementation

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-implementation/124064>\
**Category:** Rails\
**Created:** [December 24, 2007, 8:01am UTC](https://www.ruby-forum.com/t/single-table-inheritance-implementation/124064 "2007-12-24T08:01:58Z")\
**Posts on this page:** 2\
**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 24, 2007, 8:01am UTC](https://www.ruby-forum.com/t/single-table-inheritance-implementation/124064/1 "2007-12-24T08:01:58Z")

</div>

I want to change the “type” field for a table but am getting an error:  
read\_creatures.rb:19: undefined local variable or method `green\_dragon’

I’ve tried both “green\_dragon” and the class name “GreenDragon” with  
both  
giving (different) errors ☹

thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ ruby make\_creatures.rb  
– create\_table(:creatures)  
SQL (0.023529) CREATE TABLE creatures (“id” INTEGER PRIMARY KEY NOT  
NULL, “type” varchar(255) DEFAULT NULL, “life” integer DEFAULT NULL,  
“strength” integer DEFAULT NULL, “charisma” integer DEFAULT NULL,  
“weapon” integer DEFAULT NULL)  
-\> 0.1483s  
#\<Dragon:0xb7747f54 @new\_record=true, @attributes={“strength”=\>0,  
“type”=\>“Dragon”, “charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.000930) INSERT INTO creatures (“strength”, “type”,  
“charisma”,  
“life”, “weapon”) VALUES(0, ‘Dragon’, 0, 0, 0)  
#\<Dragon:0xb773e2ec @new\_record=true, @attributes={“strength”=\>0,  
“type”=\>“Dragon”, “charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.000742) INSERT INTO creatures (“strength”, “type”,  
“charisma”,  
“life”, “weapon”) VALUES(0, ‘Dragon’, 0, 0, 0)  
#\<Dragon:0xb773bb8c @new\_record=true, @attributes={“strength”=\>0,  
“type”=\>“Dragon”, “charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.000735) INSERT INTO creatures (“strength”, “type”,  
“charisma”,  
“life”, “weapon”) VALUES(0, ‘Dragon’, 0, 0, 0)  
#\<Dragon:0xb77394b8 @new\_record=true, @attributes={“strength”=\>0,  
“type”=\>“Dragon”, “charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.001781) INSERT INTO creatures (“strength”, “type”,  
“charisma”,  
“life”, “weapon”) VALUES(0, ‘Dragon’, 0, 0, 0)  
#\<Dragon:0xb7734440 @new\_record=true, @attributes={“strength”=\>0,  
“type”=\>“Dragon”, “charisma”=\>0, “weapon”=\>0, “life”=\>0}\>  
SQL (0.000739) INSERT INTO creatures (“strength”, “type”,  
“charisma”,  
“life”, “weapon”) VALUES(0, ‘Dragon’, 0, 0, 0)  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ ruby read\_creatures.rb  
Creature Load (0.006906) SELECT \* FROM creatures  
Dragon Update (0.001537) UPDATE creatures SET “charisma” = 0,  
“strength” = 0, “weapon” = 0, “life” = 0, “type” = ‘Dragon’ WHERE “id” =  
1  
Dragon Update (0.000748) UPDATE creatures SET “charisma” = 0,  
“strength” = 0, “weapon” = 0, “life” = 0, “type” = ‘Dragon’ WHERE “id” =  
2  
Dragon Update (0.000731) UPDATE creatures SET “charisma” = 0,  
“strength” = 0, “weapon” = 0, “life” = 0, “type” = ‘Dragon’ WHERE “id” =  
3  
read\_creatures.rb:19: undefined local variable or method `green_dragon' for main:Object (NameError) from read_creatures.rb:17:in`each’  
from read\_creatures.rb:17  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat read\_creatures.rb  
require ‘fileutils’  
require ‘active\_record’  
require ‘creature’  
require ‘dragon’  
require ‘green\_dragon’

ActiveRecord::Base.logger = Logger.new(STDERR)  
ActiveRecord::Base.colorize\_logging = true

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlite3”,  
:dbfile =\> “dwemthys.db”  
)

index = 0  
Creature.find(:all).each { |creature|  
if (index \> 2) then  
creature.type = green\_dragon  
creature.weapon = 1000  
end  
creature.save  
index = index+1  
}

Creature.find(:all).each { |creature|  
puts creature.inspect  
}  
thufir@arrakis ~/Desktop/dwemthys $

thanks,

Thufir

---

<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 24, 2007, 9:02am UTC](https://www.ruby-forum.com/t/single-table-inheritance-implementation/124064/2 "2007-12-24T09:02:45Z")

</div>

Ohhh, it’s a string. So, the following worked:

thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat read\_creatures.rb  
require ‘fileutils’  
require ‘active\_record’  
require ‘creature’  
require ‘dragon’  
require ‘green\_dragon’

ActiveRecord::Base.logger = Logger.new(STDERR)  
ActiveRecord::Base.colorize\_logging = true

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlite3”,  
:dbfile =\> “dwemthys.db”  
)

index = 0  
Creature.find(:all).each { |creature|  
if (index \> 2) then  
creature.type = “GreenDragon” #MUST be a string!  
creature.weapon = 1000  
end  
creature.save  
index = index+1  
}

Creature.find(:all).each { |creature|  
puts creature.inspect  
}  
thufir@arrakis ~/Desktop/dwemthys $

thanks,

Thufir
