I think that you need to modify this method
def popular_post self.posts.max_by do |a| a.likes end end
max_by
is a method for an Enumerable class and you call it on a Post class, so this will not work properly. I would suggest that you use something like posts.order('likes DESC').first
if likes
is an Integer.
Also tiny hint you don't need to use all this self
in an object and in class methods. Rails would assume that you call it on an object if this is an object method or you call it on a relation if this is a class method.