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.
0
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.
13
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:
-
Since the
::new_remember_token
is declared as of the property the classUser
itself, i.e. as a singleton method, the call to it shell be done with call to the classUser
ifself, in case whenself
isn’tUser
class, or to usingself
operator, whenself === User
. -
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 ofUser
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.