# Getters and setters problem?

**URL:** <https://www.ruby-forum.com/t/getters-and-setters-problem/93484>\
**Category:** Rails\
**Created:** [April 3, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484 "2006-04-03T22:38:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 3, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/1 "2006-04-03T22:38:05Z")

</div>

Hi list,

first evening of playing with rails, so please forgive me if I ask  
something stupid. 😉

I created a User model and tried to use ActiveRecord callbacks to  
convert the password to sha1 just before saving it. For some reason  
postgresql gives me a error because the given password is null. To  
test even further I tried to change :login too, same error happens,  
:login is empty too.

I am sure I am missing something really obvious, but I don’t see it yet.  
Here is the model code:

require “digest/sha1”

class User \< ActiveRecord::Base  
attr\_accessor :password, :login  
attr\_accessible :password, :name, :login, :email  
validates\_confirmation\_of :password  
validates\_presence\_of :name, :login, :password, :email  
validates\_uniqueness\_of :login

def before\_create  
self.password=User.hash\_password(self.password)  
self.login=“blaat”  
end  
def after\_create  
@password=nil  
end  
private  
def self.hash\_password(password)  
Digest::SHA1.hexdigest(password)  
end  
end

Thanks in advance,  
Wijnand

–  
OpenBSD needs your help improving the softwareworld, please donate:  
[http://openbsd.org/donations.html](http://openbsd.org/donations.html)

Yes big code using companies, that includes you!

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 3, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/2 "2006-04-03T23:46:28Z")

</div>

Make your database field something like hashed\_password and that should  
help  
a bit.  
See the Agile book on this.

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 4, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/3 "2006-04-04T16:00:28Z")

</div>

because ActiveRecord will automatically create the password getter and  
setter for you… but you’re overriding it. hashed\_password should be  
in  
the db so you can reference self.hashed\_password.

def before\_create  
self.hashed\_password=User.hash\_password(self.password)  
self.login=“blaat”  
end

def after\_create  
self.password = nil  
end

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 4, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/4 "2006-04-04T16:49:26Z")

</div>

Hi Brian,

On 4/4/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> because ActiveRecord will automatically create the password getter and  
> setter for you… but you’re overriding it. hashed\_password should be in  
> the db so you can reference self.hashed\_password.

Ok, but I want the column to be called password.  
You say I override it, so I should leave the  
attr\_accessor :password, :login  
line out?

> def before\_create  
> self.hashed\_password=User.hash\_password(self.password)  
> self.login=“blaat”  
> end

So what is wrong with  
self.hashed\_password=User.hash\_password(self.password)  
if the column is named password?

Please note, the  
self.login=“blaat” makes self.login nil, this was a line I used to  
test setting values.  
I expected to a new user created with a login name “blaat” and a sha1  
password.

I am trying to learn ruby (and off course, rails) so understanding is  
very important for me at this moment, much more usefull then using  
example code snippets. 🙂

Wijnand

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 3, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/5 "2006-04-03T23:55:53Z")

</div>

On 4/3/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Make your database field something like hashed\_password and that should help  
> a bit.  
> See the Agile book on this.

Yes, I used that as a base.  
But I am curious: why?

I will try your suggestion though.

Thanks for your response,  
Wijnand

–  
OpenBSD needs your help improving the softwareworld, please donate:  
[http://openbsd.org/donations.html](http://openbsd.org/donations.html)

Yes big code using companies, that includes you!

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 4, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/6 "2006-04-04T17:36:08Z")

</div>

If you want password to be your db column, then make your form field and  
attr\_writer use some other name. You can’t just always override methods  
that are created from AR reflection. It doesn’t always work and I  
suspect  
that this is one of those times.

The Agile book does spell this out in detail as to why there is a  
different  
accessor method from the database.

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 4, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/7 "2006-04-04T17:29:42Z")

</div>

On Tue, 2006-04-04 at 16:46 +0200, Wijnand W. wrote:

> line out?  
> Please note, the  
> self.login=“blaat” makes self.login nil, this was a line I used to  
> test setting values.  
> I expected to a new user created with a login name “blaat” and a sha1 password.
> 
> I am trying to learn ruby (and off course, rails) so understanding is  
> very important for me at this moment, much more usefull then using  
> example code snippets. 🙂

* * *

I think I originally used the snippets from AWDWR but this is what I  
used…(note that they are commented out since I switched to LDAP)

also note, the column in my table is hashed\_password

attr\_accessor :password  
attr\_accessible :login, :password, :name  
def before\_create  
self.hashed\_password = User.hash\_password(self.password)  
end

def after\_create  
@password = nil  
end

# This section is for users table login only.

# 

# def self.login(login, password)

# hashed\_password = hash\_password(password || “”)

# find(:first,

# :conditions =\> [“login = ? and hashed\_password = ?”, login,

hashed\_password])

# end

def try\_to\_login  
User.login(self.login, self.password)  
end

private

# This section is for users table login only.

# 

# def self.hash\_password(password)

# Digest::SHA1.hexdigest(password)

# end

Craig

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [April 4, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/getters-and-setters-problem/93484/8 "2006-04-04T17:39:11Z")

</div>

Ok, thanks for your replies guys.

Regards,  
Wijnand

On 4/4/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On Tue, 2006-04-04 at 16:46 +0200, Wijnand W. wrote:
> > 
> > > attr\_accessor :password, :login
> > 
> > I think I originally used the snippets from AWDWR but this is what I  
> > def after\_create
> > 
> > # end
> > 
> > # Digest::SHA1.hexdigest(password)

–  
OpenBSD needs your help improving the softwareworld, please donate:  
[http://openbsd.org/donations.html](http://openbsd.org/donations.html)

Yes big code using companies, that includes you!
