Dave Burt
4/3/2006 12:26:00 PM
Fred wrote:
> def crypt_unless_empty
> if password(true).empty?
> user = self.class.find(self.id)
> self.password = user.password
> else
> write_attribute "password", self.class.sha1(password(true))
> @password = nil
> end
> end
>
> is there any practical reason to use sometimes self.password and
> sometimes @password? I thought they were synonyms...
self.password = ... # calls the method "password="
It's a method call. You might assume from the name it sets @password,
but it may do other stuff as well (e.g. checks, or in this case, encrypt
the given password string).
In an ActiveRecord subclass (as this appears to be) password=(foo) is a
method that just calls write_attribute("password", foo), simply setting
the password attribute of the active record. There's no @password
involved, only @attributes["password"].
Of course, this method can be overridden, and it probably is in the
class you're looking at. Look for the line beginning "def password=" for
more clues.
Cheers,
Dave