【发布时间】:2010-11-02 03:16:08
【问题描述】:
如何编写 AR 查找查询以使结果按 has_many 关联中的记录数排序?
class User < ActiveRecord::Base
has_many :photos
end
我想做一些类似...的事情
User.find(:all, :order => photos.count)
我意识到我的发现不是有效代码。假设我有以下数据。
User 1, which has 3 photos
User 2, which has 5 photos
User 3, which has 2 photos
我希望我的发现按...的顺序将我带回用户。
User 2,
User 1,
User 3
基于用户照片的数量
【问题讨论】:
标签: ruby-on-rails activerecord