如何将此原始SQL转换为named_scope?
select d.*, count(*) shots_count
from duels d, duel_shots ds
where d.id = ds.duel_id
group by d.id
having (d.shots = 1 and shots_count >= 2) or (d.shots = 3 and shots_count >= 6)
答案 0 :(得分:0)
顺便说一下,我想你想要加入。放手一搏:
class Duel < ActiveRecord::Base
has_many :duel_shots
named_scope :blah,
:select => 'duels.*, count(duels.id) shots_count',
:joins => :duel_shots,
:group => 'duels.id',
:having => '(duels.shots = 1 AND shots_count >= 2) OR (duels.shots = 3 AND shots_count >= 6)'
end