reuse of attribute

Kind of confused about why this happened.

Initially, my "players" table had a column named "password". While learning about hashing, I had a model similar to the above, except the before_create looked like this:

  self.password = Player.hashed_password(self.password)

When I saved a player, I always got null in the password in the database.

But, if I set the hashed_password into some other attribute (self.last_name, let's say), I ended up with a hash value in the last_name column.

So all I did was to drop and create the table with the column called "hashed_password" instead, and updated the model as attached, and off I went.

I don't understand why the 'password' column kept getting nulled initially, though. I expected it would act like java; a reference to the password attribute (string) would be passed into the hashed_password method, which would return a reference to a new string, which I would happen to assign to the hashed_password attribute.

Can anyone shed a little light on what's under the covers? Just a curiousity question, it's all good after the rename.

Attachments: http://www.ruby-forum.com/attachment/2981/player.rb

I'm guessing that you may have overwritten the default AR accessor for 'password' with your call to:

attr_accessor :password

Hence, the 'password' field not being saved to the db.

A better approach, IMHO, is naming your password field differently (like password_hash) and saving it with a the below password method:

def password=(password)   self.password_hash = Digest::SHA1.hexdigest(password) end

That way, you can set a new password for the given player even after it has already been created.