# Reuse of attribute

**URL:** <https://www.ruby-forum.com/t/reuse-of-attribute/154405>\
**Category:** Rails\
**Created:** [November 28, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/reuse-of-attribute/154405 "2008-11-28T20:47:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 28, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/reuse-of-attribute/154405/1 "2008-11-28T20:47:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 29, 2008, 10:17am UTC](https://www.ruby-forum.com/t/reuse-of-attribute/154405/2 "2008-11-29T10:17:14Z")

</div>

On Nov 29, 3:47 am, Scott P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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](http://www.ruby-forum.com/attachment/2981/player.rb)
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

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.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 29, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/reuse-of-attribute/154405/3 "2008-11-29T19:45:29Z")

</div>

Erol F. wrote:

> On Nov 29, 3:47?am, Scott P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 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](http://www.ruby-forum.com/attachment/2981/player.rb)
> > 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> 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.

Yep, absolutely right–exactly what I did is re-name the db column as  
you show, and off I went.

I was thinking of them like members of a java class, I’ll have to read  
up on the attr\_accessor and how these objects actually act.

Thank you for the response.
