0

我试图在我的用户模型中链接两个 named_scopes。

首先:

  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`...

我该如何解决这个问题?

4

1 回答 1

0

改变

:order => 'total_comments desc'

: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' }
      } 
于 2010-05-25T12:25:11.627 回答