Ruby on rails构建关联范围

时间:2011-10-23 09:18:41

标签: ruby-on-rails ruby-on-rails-3 activerecord model scope

如何根据爱好者的数量​​列出索引页面中的所有游戏?感谢

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

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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