0

I have a category model, An Image model and a User model

  Category:
      has_many :images
      has_many :users, :through=>'images'


  Image: 
    belongs_to :category, :counter_cache => true
    belongs_to :user

  User:
      has_many :images


   Category.first.users //returning users but its not ordering ie :order=>' images uploaded by users in that category DESC'.
   Category.all.collect(&:users)  //returning users but its not ordering ie :order=>' images uploaded by users in that category DESC'..
  • I need list of users for that category having top 25 users having maximum number of images in that category.
  • I need top 25 users in two or more categories something like top 25 users in category 1 and 2.
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

1 Answers1

0

Try doing:

Category.first.images.group(:user_id).count(:user_id, :order=>"count_user_id desc").map {|k,v| k}.first(25)

That will give you an ordered array of the top 25 user ids posting images to the category.

To do this all in one query, i believe you would need to do a more complicated select (find by sql)

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
Ben Miller
  • 1,464
  • 12
  • 13