Forum: Ruby on Rails difference between @secure_password, secure_password

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.
74105552be623562f5da32b6e93997ac?d=identicon&s=25 Cagan Senturk (Guest)
on 2006-06-09 14:52
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
This topic is locked and can not be replied to.