2010-05-08 105 views
0

如何將此SQL轉換爲named_scope?此外,我希望總評論參數通過lambda傳遞。將SQL語句轉換爲named_scope?

「選擇用戶。,計數(),如從用戶TOTAL_COMMENTS,評論,其中(users.id = comments.user_id)和(comments.public_comment = 1)和(comments.aasm_state = '發佈')和( comments.forum_user_id爲空)group by users.id having total_comments> 25「

回答

1
named_scope :comments_count_gt, lambda { |count| 
    { :select => "users., count() as total_comments", 
    :joins => :comments, 
    :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted' } }, 
    :group => "users.id", 
    :having => "total_comments > #{count.to_i}" 
    } 
}