Help me understand Rails and attributes as class methods

idiomsrubyruby-on-rails

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 User like name, email, 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 self.remember_token and User.encrypt for accessing the class method.


My question

What's the difference between the above and the following where User, is replaced with self. ?

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

I'm a novice in the ruby language, but pretty good with javascript, so if there's a js analog it might help me to put it in those terms.

Best Answer

You're currently working on the User model within ruby as you look down you code you can see

def function

end

That creates a function that exists within the User class, so in theory from most anywhere else in your application you could do User.some_function, you only use self.some_function from within the class.

There isn't any difference between using User.encrypt and self.encrypt except that its safer, self.function means only look inside the current class.

I think you're probably getting attributes and functions mixed up because you're not seeing the () at the end of function calls, you don't actually use those in ruby unless you passing parameters to the function.

Where you're mentioning username, password, email etc those are fields associated with the database and are attributes.

The concept to understand here is that the User model represents the association with the underlying database, you can(as you have there) add extra functionality to ensure the quality of the data going in for example:

before_create :create_remember_token

That means that if ANYWHERE within you application you create a new user the model calls that function as part of the process of creating the record. Essentially by building functionality into models you can ensure that all data going into the database is treated in the same way.

Related Topic