我有一个类别模型,一个图像模型和一个用户模型
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'..
答案 0 :(得分:0)
尝试做:
Category.first.images.group(:user_id).count(:user_id, :order=>"count_user_id desc").map {|k,v| k}.first(25)
这将为您提供前25个用户ID的有序数组,将图像发布到该类别。
要在一个查询中执行此操作,我相信您需要执行更复杂的选择(通过sql查找)