2011-10-23 77 views
1

如何根據愛好者數量列出索引頁面中的所有遊戲?感謝Ruby on rails建立關聯範圍

class Game < ActiveRecord::Base 
    has_many :reverse_relationships, :dependent => :destroy, :foreign_key => "game_id", :class_name => "Relationship" 
    has_many :lovers, :through => :reverse_relationships, :source => :user_id 
end 

回答

2

嘗試是這樣的:

Game.all(:include => :lovers).sort{|a,b| b.lovers.size <=> a.lovers.size} 
+1

感謝,它的工作 –

+0

是否能解決你的問題,PLZ給予好評我的答案;-) – auralbee

+1

對不起, 「投票最多需要15聲譽」 –