How to turn this SQL query into an ActiveRecord query?
I've got a Model: Score player:string, sport:string score:integer
.
I'm currently using find_by_sql for this SQL query:
SELECT * FROM (SELECT * FROM scores ORDER BY score ASC) AS a1 GROUP BY a1.player HAVING a1.sport = 'Soccer';
Is it possible to translate the above SQL query into a Rails ActiveRecord query for portability sake?
Thanks
Score.group('player').having('sport = ?', 'Soccer').order('score')
will generate the SQL
SELECT 'scores'.* FROM 'scores' GROUP BY player HAVING sport = 'Soccer' ORDER BY score
精彩评论