将SQL语句转换为named_scope?

时间:2010-05-08 21:04:17

标签: sql mysql ruby-on-rails named-scope

如何将此SQL转换为named_scope?另外,我希望总评论参数通过lambda传递。

“选择用户。,将()计为用户的total_comments,其中的注释(users.id = comments.user_id)和(comments.public_comment = 1)和(comments.aasm_state ='已发布) ')和(comments.forum_user_id为null)由users.id分组,其total_comments> 25“

1 个答案:

答案 0 :(得分: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}"
  }
}