0

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.

1

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.

  • helpful because it's helping me formulate my question. will edit my original question for clarity. – tim Dec 11 '13 at 18:45
  • No problem I started rails a few months ago and the learning curve is steep! – Mrk Fldig Dec 11 '13 at 18:47
  • 1
    @tim please note that if the edit to the question is a different question than the one currently being asked, it may be better to ask a new question. Each question should ask only one question, and the answers to that question should answer only that question. – user40980 Dec 11 '13 at 18:52
  • @MichaelT ok. i'll make sure it gets straightened out once i figure out what im trying to ask. thanks to Mrk Fldig for helping nudge me in that direction. – tim Dec 11 '13 at 18:55
  • 1
    @tim one of the hardest switches in thinking on Stack Exchange is how this isn't a forum - the back and forth takes place in comments or new questions if the issue brought up is a new question itself. When this lightbulb goes on as to how things work, the system becomes much easier to work in and with (and you find yourself asking better questions for more rep). – user40980 Dec 11 '13 at 18:58
1

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. What's the difference between the above and the following where User. is replaced with self.

Functionally there is no difference between User, and self in the context of class User, but the differs to each other in the context of User class instance. So for the context User class you are able to call the ::new_remember_token methods as both of User class, and self, and they return the same (for the context) results.

self.new_remember_token

User.new_remember_token

However, as you correctly denote, that attribute of the User model is referred to both with self, and with User. So I believe, the author put that difference by the two main reasons:

  1. Since the ::new_remember_token is declared as of the property the class User itself, i.e. as a singleton method, the call to it shell be done with call to the class User ifself, in case when self isn't User class, or to using self operator, when self === User.

  2. In order to split meanings of real properties that is mirrored unto DB, and the class singleton methods that have no that function. So you saw:

    self.remember_token = ... # call to property 'remember_token'
    
    User.new_remember_token # call to User model's singleton method
    

    However, the #new_remember_token could be declared as of User instance method as follows:

    def new_remember_token
       ...
    end
    

    And call to it shell be done with:

    self.remember_token = self.encrypt(self.new_remember_token)
    

    But the author have in mind that the purpose of the ::new_remember_token method isn't the same as of attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.