Question

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.

Was it helpful?

Solution 2

OTHER TIPS

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

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)


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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top