如何解决MySQL错误?链接命名作用域

如何解决MySQL错误?链接命名作用域,mysql,ruby-on-rails,named-scope,Mysql,Ruby On Rails,Named Scope,我试图在我的用户模型中链接两个命名的_作用域 第一: named_scope :commentors, lambda { |*args| { :select => 'users.*, count(*) as total_comments', :joins => :comments, :conditions => { :comments => { :public_comment => 1, :aasm_state =&g

我试图在我的用户模型中链接两个命名的_作用域

第一:

  named_scope :commentors, lambda { |*args|
      { :select => 'users.*, count(*) as total_comments',
        :joins => :comments,
        :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
        :group => 'users.id',
        :having => ['total_comments > ?', args.first || 0],
        :order => 'total_comments desc' }
      } 
第二点:

  named_scope :not_awarded_badge, lambda { |badge_id|
    { :include => :awards,
      :conditions => [ "? not in (select awards.badge_id from awards where awards.user_id = users.id)", badge_id ]
    }
  }
我试图将这两个链接如下:

User.commentors(25).not_awarded_badge(1)
但是,我得到以下错误:

Mysql::Error: Unknown column 'total_comments' in 'order clause': SELECT `users`.`id`...
如何解决此问题?

更改

:order => 'total_comments desc'

它应该是

 named_scope :commentors, lambda { |*args|
      { :select => 'users.*, count(*) as total_comments',
        :joins => :comments,
        :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
        :group => 'users.id',
        :having => ['total_comments > ?', args.first || 0],
        :order => 'count(*) desc' }
      } 

你也必须这样做:拥有
 named_scope :commentors, lambda { |*args|
      { :select => 'users.*, count(*) as total_comments',
        :joins => :comments,
        :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
        :group => 'users.id',
        :having => ['total_comments > ?', args.first || 0],
        :order => 'count(*) desc' }
      }