简体   繁体   English

按点排序用户(使用优点宝石)

[英]Sorting users by points (using merit gem)

What's the best way to order a users list by their points generated by the merit gem? 通过优点宝石生成的点来订购用户列表的最佳方法是什么? I'm having difficulty doing so because merit probably doesn't save it's data in the db. 我很难这样做,因为优点可能不会在数据库中保存它的数据。

So far, I'm caching merit points in the users table, I was just wondering if there's a better way. 到目前为止,我正在缓存用户表中的优点,我只是想知道是否有更好的方法。

In your User model, you can define the default scope: 在User模型中,您可以定义默认范围:

        scope :ordered, ->{ joins('LEFT JOIN merit_scores ON ' \
                                   merit_scores.sash_id = "users".sash_id ' \
                                  'LEFT JOIN merit_score_points ON merit_score_points.score_id = merit_scores.id')
                           .group('"users".id')
                           .order('COALESCE(SUM(num_points), 0) DESC') }

This will exclude users with no points 这将排除没有积分的用户

User.joins('RIGHT JOIN merit_scores ON merit_scores.sash_id = users.sash_id RIGHT JOIN merit_score_points ON merit_score_points.score_id = merit_scores.id')
    .group('users.id', 'merit_scores.sash_id')
    .order('SUM(num_points) DESC').limit(10)

Firstly, you've been down-voted by someone because your question is very ambiguous. 首先,你被某人拒绝了,因为你的问题很模糊。 SO is for programming-specific questions (you need to include code) SO是针对特定于编程的问题(您需要包含代码)


Order 订购

In terms of an answer, it will be to use the .order ActiveRecord function: 在答案方面,它将使用.order ActiveRecord函数:

User.merits.order(:vote)

As you've not provided much code, any further help I try to provide would just be guesswork - no help to anyone. 由于您没有提供太多代码,我尝试提供的任何进一步帮助都只是猜测 - 对任何人都没有帮助。 Maybe you could update your answer with some more details? 也许您可以通过更多细节更新您的答案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM