This is from the Hartl Rails tutorial. We are automatically logging in a user in the process of creating that user with a callback to
#create_remember_token. It works great but there are some things, I am not clear on. Please bear with me as I'm thoroughly confused to the point that I don't even know how to ask my question.
I have a user model:
class User<ActiveRecord::Base ... ... before_create :create_remember_token def User.new_remember_token SecureRandom.urlsafe_base64 end def User.encrypt(token) Digest::SHA1.hexdigest(token.to_s) end private def create_remember_token self.remember_token = User.encrypt(User.new_remember_token) end
I am confused about the
::new_remember_token and the encrypt being methods attached to
User. It looks as though they are attributes of
password etc, because they are being accessed with dot notation. But they are defined like methods and seem to return a computed value. I don't understand, why an attribute of the
User model is referred to with
self for assigning
User.encrypt for accessing the class method.
What's the difference between the above and the following where
User, is replaced with
class User<ActiveRecord::Base ... ... before_create :create_remember_token def new_remember_token SecureRandom.urlsafe_base64 end def encrypt(token) Digest::SHA1.hexdigest(token.to_s) end private def create_remember_token self.remember_token = self.encrypt(self.new_remember_token) end