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
Yes big code using companies, that includes you!
Make your database field something like hashed_password and that should
help
a bit.
See the Agile book on this.
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
Hi Brian,
On 4/4/06, Brian H. [email protected] 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
On 4/3/06, Brian H. [email protected] 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!
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.
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
Ok, thanks for your replies guys.
Regards,
Wijnand
On 4/4/06, Brian H. [email protected] 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!