Zuhaib Ali
Zuhaib Ali

Reputation: 3384

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.

Upvotes: 0

Views: 253

Answers (4)

nahankid
nahankid

Reputation: 221

In your User model, you can define the default scope:

        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') }

Upvotes: 3

Arugin
Arugin

Reputation: 1947

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)

Upvotes: 0

Zuhaib Ali
Zuhaib Ali

Reputation: 3384

The closest solution I have found is https://github.com/tute/merit/wiki/How-to-show-a-points-leaderboard

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76784

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)


Order

In terms of an answer, it will be to use the .order ActiveRecord function:

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?

Upvotes: -1

Related Questions