Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to approach GROUP BY in PostgreSQL using Rails?

I have a Rails 3 app which keeps high scores. I'm hosting it on Heroku which uses postgresql as the db.

I need to extract the top scores from the scores table. The table has columns score and user_id. It was working in mysql with the following:

Score.order('score DESC').group('user_id').limit(25)

This ranks each user's top score.

When I put the app on Heroku, I get the following psql error PGError: ERROR: column "scores.id" must appear in the GROUP BY clause or be used in an aggregate function

I've read around but haven't found a clear answer. What is the most optimal way to recreate the above query to work with PostgreSQL?

Thanks!

Tim

like image 838
Tim Avatar asked Dec 27 '10 23:12

Tim


1 Answers

That means your select query is selecting the "id" column but not including it in the group by clause. I'm not familiar with rails but could it be selecting * or all columns?

like image 61
blahter Avatar answered Nov 08 '22 21:11

blahter