Single Table Inheritance implementation

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 :frowning:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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)
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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:ineach’
from read_creatures.rb:17
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
}
[email protected] ~/Desktop/dwemthys $

thanks,

Thufir

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

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
}
[email protected] ~/Desktop/dwemthys $

thanks,

Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs