【问题标题】:Ruby on rails building association scopeRuby on Rails 构建关联范围
【发布时间】:2011-10-23 09:18:40
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    试试这样的:

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

    【讨论】:

    • 抱歉,“投票需要 15 声望”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多