2011-07-30 109 views
0

我有一个型号:Score player:string, sport:string score:integer如何将此SQL查询转换为ActiveRecord查询?

我目前使用的find_by_sql此SQL查询:

SELECT * FROM (SELECT * FROM scores ORDER BY score ASC) AS a1 GROUP BY a1.player HAVING a1.sport = 'Soccer';

是否有可能上面的SQL查询到一个Rails的ActiveRecord查询转换为便携的缘故?

感谢

回答

0
Score.group('player').having('sport = ?', 'Soccer').order('score') 

将生成SQL

SELECT 'scores'.* FROM 'scores' GROUP BY player HAVING sport = 'Soccer' ORDER BY score