如何将find_by_sql语句转换为named_scope?

发布于 2024-09-02 09:04:53 字数 1362 浏览 7 评论 0原文

如何将以下内容转换为named_scope?

  def self.commentors(cutoff=0)
    return User.find_by_sql("select users.*, count(*) as total_comments from users, comments 
      where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null) 
      group by users.id having total_comments > #{cutoff} order by total_comments desc")
  end

这就是我现在所拥有的,但它似乎不起作用:

  named_scope :commentors, lambda { |count=0|
    { :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 > #{count.to_i}",
      :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 => 'total_comments desc' }
      }  

How do I translate the following into a named_scope?

  def self.commentors(cutoff=0)
    return User.find_by_sql("select users.*, count(*) as total_comments from users, comments 
      where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null) 
      group by users.id having total_comments > #{cutoff} order by total_comments desc")
  end

Here's what I have right now but it doesn't seem to work:

  named_scope :commentors, lambda { |count=0|
    { :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 > #{count.to_i}",
      :order => "total_comments desc"
    }
  }

Ulimately, this is what worked;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-09-09 09:04:53

您遇到的问题是因为您用 comments 的内部联接替换了从 users, comments 中进行选择。要将 SQL 正确转换为 named_scope,请使用以下命令:

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

并且 lambda 参数不能有默认值,因此不能使用 |cutoff = 0|

The problem you have is because you replaced selecting from users, comments with an inner join with comments. To properly translate the SQL to a named_scope, use this:

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

And you can't have a default value for lambda parameters, so you can't use |cutoff = 0|.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文