Getters and setters problem?


#1

Hi list,

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

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

Yes big code using companies, that includes you!


#2

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


#3

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


#4

Hi Brian,

On 4/4/06, Brian H. 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. :slight_smile:

Wijnand


#5

On 4/3/06, Brian H. 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

Yes big code using companies, that includes you!


#6

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.


#7

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. :slight_smile:


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


#8

Ok, thanks for your replies guys.

Regards,
Wijnand

On 4/4/06, Brian H. 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

Yes big code using companies, that includes you!