jeudi 3 avril 2014

activerecord - getting the most popular category


Vote count:

0




I have a model Category and class method tickets_num which return the number of tickets that belongs to this category: Category.first.tickets_num # => 2



class Category < ActiveRecord::Base
has_many :tickets

def tickets_num
self.tickets.count
end
end


I would like to do controller method "popular" which will return me 3 categories with the highest number of tickets_num , how can I do it in the most elegant way?



def popular
@categories = Category.all.order(tickets_num).limit(3)
end


my method doesn't work.



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire