2010-05-25 47 views
0

我想在我的用戶模型中鏈接兩個named_scopes。如何解決MySQL錯誤?鏈接命名示波器

第一:

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

我該如何解決這個問題?

回答

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' } 
     } 
+0

你要爲做同樣的:具有 – keruilin 2010-05-25 22:35:08