【问题标题】:Paginate ordering doesn't work when using find_all_by_completed(false)使用 find_all_by_completed(false) 时分页排序不起作用
【发布时间】:2010-01-22 13:29:04
【问题描述】:

在我的项目模型中

  def incomplete
  @clients = current_user.clients.find_all_by_completed(false).paginate
  (:page => params[:page], :per_page => 10, :order => 'started_on DESC')
  end

由于某种原因,它不会按started_on 降序排列。然而,排序工作在另一种方法中

def all
@clients = current_user.clients.paginate(:page => params[:page], :per_page => 25, :order => 'started_on DESC')
end

所以我假设使用 find_all_by_completed 会引发分页。我正在使用将分页顺便说一句。有什么帮助吗?

【问题讨论】:

    标签: ruby-on-rails pagination will-paginate


    【解决方案1】:

    尝试显式传递条件:

    @clients = current_user.clients.paginate(
        :conditions => {:completed => false}, 
        :page => params[:page], :per_page => 10, 
        :order => 'started_on DESC')
    

    【讨论】:

    • 另外,查看命名范围,您可以这样做:current_user.clients.completed.paginate(...)。
    • 好点。一旦某个条件对我有用,如果我打算多次使用它,我通常会将它放到一个范围内。
    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多