【问题标题】:How to Resolve MySQL Error? Chaining Named Scopes如何解决 MySQL 错误?链接命名范围
【发布时间】:2010-05-25 12:19:02
【问题描述】:

我正在尝试在我的用户模型中链接两个 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`...

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    改变

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

    【讨论】:

    • 你必须为 :have 做同样的事情
    猜你喜欢
    • 2010-11-01
    • 1970-01-01
    • 2011-05-25
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2015-07-21
    • 2014-01-10
    相关资源
    最近更新 更多