Rails - Class and Object Method Difference

When I started my job as Ruby On Rails developer , I often confused when calling a method from model. I often get an undefined method error. At that time, I haven't understood about the difference of class and object method. Therefore, I treated both of them as the object method. But now I have understood their difference, especially the way to call them. Here are the explanation.

My experience showed that the fastest way to understand new thing is through seeing how they worked on example. Let's start with example of class method. Supposed that we have a User model on our application.

class User
def self.get_username_list #To call this method : User.get_username_list
 Rails.cache.fetch "username_list" do

get_username_list is a class method. Class method is signed by self. at the beginning of its name. You can see the way to call this method on the coment next to that method name. You can call that way either on controllers, models, or view.

What about object method ? Object method name isn't started with self. To call object method, you need to create an object from the class first. Here's the example of object method and the way to call it.

class User
def is_active?(user)
 if user.present? and user.confirmation_codes.blank?
   return true
   return false

How to call is_active? method? To call it, you need to create the object of user first, then use this object to call that method. Here's the example how to call that method.
@current_user = User.find(2) #create an object from User class
@current_user.is_active? #use the object created to call object method
#this won't work : User.is_active? because it is an object method

Now the question is, when should we use class method ? when should we use object method ? For this question, I think the answer is clear. We need to know the purpose of the method. Back to the example, the first one's purpose is to retrieve all username list on database. Therefore, no need to instantiate a User object.

Opposite to the second one, where its purpose is to check a user is active or not. We need to know which users need to be checked. To know it, we do a query to find a user. The query will return a User object. So is_active? must be an object method.