Rails 如何得到某个一个最多的多对多的子类?

chrishyman · August 11, 2014 · Last by hz_qiuyuanxin replied at August 11, 2014 · 1869 hits

标题说的太模糊了,举个例子,我有一个 Post Model 里头有一个 Author Model,关系是多对多的。

如果我要做一个排列,来排列出创作最多 Post 的 Author。有什么比较好的方法?

authors.sort_by{ |author| author.posts.count }.reverse 大概是这样?没测试...

给每个Author加个post_count属性

chrishyman closed this topic. 15 Mar 14:15
You need to Sign in before reply, if you don't have an account, please Sign up first.