我有一个模型:Score player:string, sport:string score:integer
。
我目前正在使用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 :(得分:0)
Score.group('player').having('sport = ?', 'Soccer').order('score')
将生成SQL
SELECT 'scores'.* FROM 'scores' GROUP BY player HAVING sport = 'Soccer' ORDER BY score