editing a row as an object

Why isn't this changing the "strength" field in the db? There seems to be a disconnect between the row and the object :frowning:

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ ruby read_creatures.rb   Creature Load (0.005096) SELECT * FROM creatures #<Creature:0xb7747b58 @attributes={"strength"=>"0", "id"=>"1", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50> #<Creature:0xb7747b1c @attributes={"strength"=>"0", "id"=>"2", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50> #<Creature:0xb7747af4 @attributes={"strength"=>"0", "id"=>"3", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50> #<Creature:0xb7747acc @attributes={"strength"=>"0", "id"=>"4", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50> #<Creature:0xb7747aa4 @attributes={"strength"=>"0", "id"=>"5", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50> thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat read_creatures.rb require 'fileutils' require 'active_record' require 'Creature' require 'Dragon'

ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = true

ActiveRecord::Base.establish_connection(     :adapter => "sqlite3",     :dbfile => "dwemthys.db" )

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

thanks,

Thufir

Why isn't this changing the "strength" field in the db? There seems
to be a disconnect between the row and the object :frowning:

Umm, because you've commented out creature.save ?

Fred

Umm, because you've commented out creature.save ?

Pardon, forgot about that. When it's uncommented it's almost like the Creature instance has two "strength" attributes: in the db and @strength :frowning:

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ ruby read_creatures.rb   Creature Load (0.005061) SELECT * FROM creatures #<Creature:0xb77959fc @attributes={"strength"=>"0", "id"=>"1", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50>   Creature Update (0.001530) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 1 #<Creature:0xb77959c0 @attributes={"strength"=>"0", "id"=>"2", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50>   Creature Update (0.000717) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 2 #<Creature:0xb7795998 @attributes={"strength"=>"0", "id"=>"3", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50>   Creature Update (0.000700) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 3 #<Creature:0xb7795970 @attributes={"strength"=>"0", "id"=>"4", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50>   Creature Update (0.000720) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 4 #<Creature:0xb7795948 @attributes={"strength"=>"0", "id"=>"5", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}, @strength=50>   Creature Update (0.000709) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 5 thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ sqlite3 dwemthys.db SQLite version 3.4.1 Enter ".help" for instructions

SELECT * FROM creatures;

1|0|0|0|0 2|0|0|0|0 3|0|0|0|0 4|0|0|0|0 5|0|0|0|0

.quit

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $

thanks,

Thufir

Umm, because you've commented out creature.save ?

Pardon, forgot about that. When it's uncommented it's almost like the Creature instance has two "strength" attributes: in the db and @strength :frowning:

Is save failing (ie returning false) ? What's in creatures.rb ?

Fred

Is save failing (ie returning false) ? What's in creatures.rb ?

The save, or update of a row, seems to work. However, it doesn't seem to update with the expected data. That is, it updates a value of zero, based on the output as I interpret it.

Pardon, below is what I meant to post from the get-go:

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ sqlite3 dwemthys.db SQLite version 3.4.1 Enter ".help" for instructions

SELECT * FROM creatures;

1|0|0|0|0 2|0|0|0|0 3|0|0|0|0 4|0|0|0|0 5|0|0|0|0

.quit

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ ruby read_creatures.rb   Creature Load (0.006638) SELECT * FROM creatures #<Creature:0xb7773a78 @attributes={"strength"=>"0", "id"=>"1", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}>   Creature Update (0.001596) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 1 #<Creature:0xb7773a3c @attributes={"strength"=>"0", "id"=>"2", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}>   Creature Update (0.000731) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 2 #<Creature:0xb7773a14 @attributes={"strength"=>"0", "id"=>"3", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}>   Creature Update (0.000721) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 3 #<Creature:0xb77739ec @attributes={"strength"=>"0", "id"=>"4", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}>   Creature Update (0.000736) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 4 #<Creature:0xb77739c4 @attributes={"strength"=>"0", "id"=>"5", "charisma"=>"0", "life"=>"0", "weapon"=>"0"}>   Creature Update (0.000727) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 0, "life" = 0 WHERE "id" = 5 thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ sqlite3 dwemthys.db SQLite version 3.4.1 Enter ".help" for instructions

SELECT * FROM creatures;

1|0|0|0|0 2|0|0|0|0 3|0|0|0|0 4|0|0|0|0 5|0|0|0|0

.quit

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat read_creatures.rb require 'fileutils' require 'active_record' require 'Creature' require 'Dragon'

ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = true

ActiveRecord::Base.establish_connection(     :adapter => "sqlite3",     :dbfile => "dwemthys.db" )

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

thanks,

Thufir

Is save failing (ie returning false) ? What's in creatures.rb ?

The save, or update of a row, seems to work. However, it doesn't
seem to update with the expected data. That is, it updates a value of zero, based on the output as I interpret it.

Assuming this is http://dwemthys.googlecode.com/svn/trunk/Creature.rb
then your problem is that you're doing stuff like
attr_accessor :life, :strength, :charisma, :weapon This will overwrite the accessors ActiveRecord already provides and
stop things working. Also your app will die when you deploy it on a case sensitive file
system because you've capitalized your file names.

Fred

Yes, your assumption is correct :slight_smile:

I've been planning to change the case for the filenames.

Instead of attr_accessor, how would I access the a Creature's attributes in this example? I wasn't planning on a rails app, but was just poking ActiveRecord.

thanks,

Thufir

Got it :slight_smile:

Thanks, I took out the attr_accessor and just *acted* like it was still there. This is the magic of ActiveRecord? Kinda weird, but it worked:

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ ruby read_creatures.rb   Creature Load (0.017017) SELECT * FROM creatures #<Creature:0xb77b0c98 @attributes={"strength"=>"0", "id"=>"1", "charisma"=>"0", "life"=>"0", "weapon"=>1000}>   Creature Update (0.003406) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 1000, "life" = 0 WHERE "id" = 1 #<Creature:0xb77b0c5c @attributes={"strength"=>"0", "id"=>"2", "charisma"=>"0", "life"=>"0", "weapon"=>1000}>   Creature Update (0.000733) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 1000, "life" = 0 WHERE "id" = 2 #<Creature:0xb77b0c34 @attributes={"strength"=>"0", "id"=>"3", "charisma"=>"0", "life"=>"0", "weapon"=>1000}>   Creature Update (0.000757) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 1000, "life" = 0 WHERE "id" = 3 #<Creature:0xb77b0c0c @attributes={"strength"=>"0", "id"=>"4", "charisma"=>"0", "life"=>"0", "weapon"=>1000}>   Creature Update (0.000733) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 1000, "life" = 0 WHERE "id" = 4 #<Creature:0xb77b0be4 @attributes={"strength"=>"0", "id"=>"5", "charisma"=>"0", "life"=>"0", "weapon"=>1000}>   Creature Update (0.000718) UPDATE creatures SET "charisma" = 0, "strength" = 0, "weapon" = 1000, "life" = 0 WHERE "id" = 5 thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ sqlite3 dwemthys.db SQLite version 3.4.1 Enter ".help" for instructions

SELECT * FROM creatures;

1|0|0|0|1000 2|0|0|0|1000 3|0|0|0|1000 4|0|0|0|1000 5|0|0|0|1000

.quit

thufir@arrakis ~/Desktop/dwemthys $ thufir@arrakis ~/Desktop/dwemthys $ cat read_creatures.rb require 'fileutils' require 'active_record' require 'creature' require 'dragon'

ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = true

ActiveRecord::Base.establish_connection(     :adapter => "sqlite3",     :dbfile => "dwemthys.db" )

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

-Thufir

Thanks. These commands are similar, if not identical, to what's available from the script/console interface.

-Thufir

Assuming this is http://dwemthys.googlecode.com/svn/trunk/Creature.rb then your problem is that you're doing stuff like attr_accessor :life, :strength, :charisma, :weapon This will overwrite the accessors ActiveRecord already provides and stop things working.

Yes, your assumption is correct :slight_smile:

I've been planning to change the case for the filenames.

Instead of attr_accessor, how would I access the a Creature's attributes in this example? I wasn't planning on a rails app, but was just poking ActiveRecord.

You don't need attr_accessor because AR already provides accessors for you.

Fred