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:

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

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

thanks,

Thufir

On 23 Dec 2007, at 07:43, Thufir wrote:

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

On Sun, 23 Dec 2007 10:01:21 +0000, Frederick C. wrote:

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:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ sqlite3 dwemthys.db
SQLite version 3.4.1
Enter “.help” for instructions
sqlite>
sqlite> 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
sqlite>
sqlite> .quit
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $

thanks,

Thufir

On 23 Dec 2007, at 10:22, Thufir wrote:

@strength :frowning:
Is save failing (ie returning false) ? What’s in creatures.rb ?

Fred

On Sun, 23 Dec 2007 10:43:05 +0000, Frederick C. wrote:

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:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ sqlite3 dwemthys.db
SQLite version 3.4.1
Enter “.help” for instructions
sqlite> 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
sqlite> .quit
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ sqlite3 dwemthys.db
SQLite version 3.4.1
Enter “.help” for instructions
sqlite> 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
sqlite> .quit
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
}
[email protected] ~/Desktop/dwemthys $

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:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ sqlite3 dwemthys.db
SQLite version 3.4.1
Enter “.help” for instructions
sqlite> 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
sqlite> .quit
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
}
[email protected] ~/Desktop/dwemthys $

-Thufir

On 24 Dec 2007, at 01:21, Thufir wrote:

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, that’s the magic of ActiveRecord.

On Dec 24, 2007 3:51 PM, Thufir [email protected] wrote:

Creature Load (0.017017) SELECT * FROM creatures
Creature Update (0.000757) UPDATE creatures SET “charisma” = 0,
[email protected] ~/Desktop/dwemthys $ sqlite3 dwemthys.db
[email protected] ~/Desktop/dwemthys $ cat read_creatures.rb
:adapter => “sqlite3”,

-Thufir


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

to create it from scratch:

Creature.new({:strength => 50})

To update a single attribute:

creature = Creature.find(id)
creature.update_attribute(“strengh”,50)

To update a whole heap of attributes:

creature = Creature.find(id)
create.update_attributes({:strength => 50, :life => 20})

On Dec 24, 2007 3:25 PM, Thufir [email protected] wrote:


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

On Mon, 24 Dec 2007 01:27:46 +0000, Frederick C. wrote:

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.

thanks,

Thufir

On 24 Dec 2007, at 04:55, Thufir wrote:

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

On Mon, 24 Dec 2007 16:46:37 +1030, Ryan B. wrote:

creature = Creature.find(id)
creature.update_attribute(“strengh”,50)

To update a whole heap of attributes:

creature = Creature.find(id)
create.update_attributes({:strength => 50, :life => 20})

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

-Thufir

The script/console interface is exactly the same environment you’re
given in
the application, minus there actually being a real request generating
the
code (although that can be faked too!)

Consider this my last post for the next two days.

Merry Christmas to everyone.

On Dec 24, 2007 9:42 PM, Frederick C. [email protected]
wrote:

attr_accessor :life, :strength, :charisma, :weapon This will
poking

Thufir


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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