Forum: Ruby on Rails Getters and setters problem?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b91b9685655b5e8a9dd75c184fc5d40?d=identicon&s=25 Wijnand Wiersma (Guest)
on 2006-04-03 22:38
(Received via mailing list)
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!
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-04-03 23:46
(Received via mailing list)
Make your database field something like hashed_password and that should
help
a bit.
See the Agile book on this.
1b91b9685655b5e8a9dd75c184fc5d40?d=identicon&s=25 Wijnand Wiersma (Guest)
on 2006-04-03 23:55
(Received via mailing list)
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!
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-04-04 16:00
(Received via mailing list)
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
1b91b9685655b5e8a9dd75c184fc5d40?d=identicon&s=25 Wijnand Wiersma (Guest)
on 2006-04-04 16:49
(Received via mailing list)
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. :-)

Wijnand
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-04 17:29
(Received via mailing list)
On Tue, 2006-04-04 at 16:46 +0200, Wijnand Wiersma 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
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-04-04 17:36
(Received via mailing list)
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.
1b91b9685655b5e8a9dd75c184fc5d40?d=identicon&s=25 Wijnand Wiersma (Guest)
on 2006-04-04 17:39
(Received via mailing list)
Ok, thanks for your replies guys.

Regards,
Wijnand

On 4/4/06, Brian Hogan <bphogan@gmail.com> wrote:
> > On Tue, 2006-04-04 at 16:46 +0200, Wijnand Wiersma 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!
This topic is locked and can not be replied to.