Hello,
I have a User object that extends ActiveRecord::Base.
And I have the following defined in it:
…
after_validation :crypt_password
…
def crypt_password
write_attribute(“secure_password”, User.encrypt(password))
self.secure_password = User.encrypt(password)
end
…
And this works. Where I am getting confused about is the difference
between the following:
secure_password,
@secure_password
self.secure_password
Because these versions of crypt_password method don’t work:
def crypt_password
secure_password = User.encrypt(password)
end
def crypt_password
@secure_password = User.encrypt(password)
end
When I debug the app by placing a breakpoint right after the assigment
operation
in crypt_password, and ask for values:
@secure_password returns ‘nil’
but
secure_password returns the encrypted password…
Which is very puzzling.
First of all, I thought @ was used for instance variables. So
@secure_password should be the one with the encrypted value assigned not
secure_password…
Let’s forget that…If secure_password contains the encrypted value as I
see on the console, why would secure_password = User.encrypt(password)
call not do what I expect it to do?
Obviously, I’m pretty confused about the usage of @ vs. non-@, self,
etc…
Thanks,
Cagan