开发者

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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜