Как устранить ошибку MySQL? Цепочка именованных областей - PullRequest
0 голосов
/ 25 мая 2010

Я пытаюсь связать два named_scopes в моей модели User.

Первый:

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

Как я могу решить эту проблему?

1 Ответ

0 голосов
/ 25 мая 2010

изменение

: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' }
      } 
...