Rails :HOw to order a table by associated model
问题 I think the problem is trivial. I have two models: User and Betting. User has_many :bettings Betting belongs_to :user I just want to get the users ordered by who made more bettings. 回答1: Are you on Rails2 or Rails3? On Rails3, you can use the Ruby sort method and something like: User.includes(:bettings).sort{|x,y| x.bettings.size <=> y.bettings.size} Of course, in this case the sorting occurs after the SQL request, which is not optimum if you have large tables… I'm trying to figure how to do