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. On 4/3/06, Wijnand Wiersma <wwiersma@gmail.com> wrote:> > 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! > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060403/f3fcb7c7/attachment.html
On 4/3/06, Brian Hogan <bphogan@gmail.com> 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!
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 On 4/3/06, Wijnand Wiersma <wwiersma@gmail.com> wrote:> > On 4/3/06, Brian Hogan <bphogan@gmail.com> 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! > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/e01952a8/attachment.html
Hi Brian, On 4/4/06, Brian Hogan <bphogan@gmail.com> 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" > endSo 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 Tue, 2006-04-04 at 16:46 +0200, Wijnand Wiersma wrote:> Hi Brian, > > On 4/4/06, Brian Hogan <bphogan@gmail.com> 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. :-)---- 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
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 4/4/06, Craig White <craigwhite@azapple.com> wrote:> > On Tue, 2006-04-04 at 16:46 +0200, Wijnand Wiersma wrote: > > Hi Brian, > > > > On 4/4/06, Brian Hogan <bphogan@gmail.com> 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. :-) > ---- > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/7e276fb7/attachment.html
Ok, thanks for your replies guys. Regards, Wijnand On 4/4/06, Brian Hogan <bphogan@gmail.com> wrote:> 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 4/4/06, Craig White <craigwhite@azapple.com> wrote: > > > > On Tue, 2006-04-04 at 16:46 +0200, Wijnand Wiersma wrote: > > > Hi Brian, > > > > > > On 4/4/06, Brian Hogan <bphogan@gmail.com> 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. :-) > > ---- > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- OpenBSD needs your help improving the softwareworld, please donate: http://openbsd.org/donations.html Yes big code using companies, that includes you!