何よりもまず:名前付きスコープは死んでいる
次のことをお勧めします。
class Players < ActiveRecord::Base
....
def self.with_win_count
#inner join if you want to filter out those who doesn't have any match, otherwise is should be left outer
joins("inner join (select Player_ID, count(Match_ID) as win_count from Results where Win = true group by Player_ID) t on (t.Player_ID = Player.ID)")
end
end
#somewhere in controller you would do
best_players = Player.with_win_count.where("win_count > ?", [10]).order("win_count DESC")
結果テーブルに含める場合:
class Results < ActiveRecord::Base
....
def self.with_win_count
joins("inner join (select Player_ID, count(Match_ID) as win_count from Results where Win = true group by Player_ID) t on (t.Player_ID = Results.Player_ID)")
end
end
これはうまくいくでしょうが、私はそれが少し醜いと思います。
また、ネストされたクエリで条件付きカウントが必要な場合、簡単な方法は次のようになります。
class Players < ActiveRecord::Base
....
def self.with_match_count(conditions)
#inner join if you want to filter out those who doesn't have any match, otherwise is should be left outer
joins("inner join (select Player_ID, count(Match_ID) as match_count from Results where #{conditions} group by Player_ID) t on (t.Player_ID = Player.ID)")
end
end
some_players = Player.with_match_count("Win = true").where("match_count > ?", [10]).order("match_count DESC")
!!! conditions
引数がユーザー入力から直接構築される場合は、SQLインジェクションが発生しやすいことに注意してください。