如何将这个原始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)发布于 2010-12-10 02:37:47
顺便说一句,我觉得你想加入。试一试:
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)'
endhttps://stackoverflow.com/questions/4394943
复制相似问题