2010-12-09 15 views
0

Wie kann ich dieses rohe SQL in ein named_scope umwandeln?Raw SQL zu 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) 

Antwort

0

Ich denke, Sie wollen einen Beitritt, übrigens. Geben Sie dieses ein:

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