help me understand Rails and attributes as class methods

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:

  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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật