如何获得相关的模型(滑轨)的最高计数? [英] How to get highest count of associated model (Rails)?

查看:140
本文介绍了如何获得相关的模型(滑轨)的最高计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用户的has_many解决方案

A User has_many Solutions

我如何订购的用户被那些最解决方案?

How do I order Users by those with the most Solutions?

我试图找到前十名的用户,但我不知道如何最整洁或有效的方法来做到这一点?

I'm trying to find the top ten users but I'm not sure how the most tidy or efficient way to do this?

有没有人有一个例子,是不是太耗费计算?

Does anyone have an example that isn't too computationally expensive?

推荐答案

如果你真的想这样做的一个快速方法,把 counter_cache 在用户的解决方案(有一个 solutions_count 列在用户),并以通过该列。你并不需要管理的柜台,因为轨道会为你。你可以阅读更多关于 counter_cache Rails的指南

If you really want a fast way of doing it, put a counter_cache on a users' solutions (have a solutions_count column in your User) and order by that column. You don't need to manage that counter, because rails does it for you. You can read more about counter_cache in the Rails Guides

这篇关于如何获得相关的模型(滑轨)的最高计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆