sql – 尽管需要’will_paginate / array’,但数组不能与will_paginate一起使用?

我有一个搜索模型,构建一个基本上是原始SQL的字符串.此字符串通过find_by_sql调用运行,并在下面第5行返回到控制器(@ search.query< – query是返回find_by_sql结果的方法):

1  def roster_builder
2    authorize! :access_roster_builder, current_coach
3    @search = Search.new(sport: current_coach.primary_sport, conditions: params[:search])
4
5    @athletes = @search.query.page(params[:page]).per_page(8)
6
7    if params[:refresh]
8      render "athletes_refresh" 
9    else
10     render "athletes" if request.format.js?
11   end
12 end

这会返回一个数组,但是我收到此错误:

NoMethodError (undefined method `page' for #<Array:0x00000105b7add0>)

所以我发现插入以下内容应该解决这个问题:

require 'will_paginate/array'

我已经尝试将它放在我的控制器中,并且will_paginate.rb初始化文件无效.我甚至对find_by_sql结果做了.to_a – 同样的错误.究竟是什么问题?我正在使用will_paginate 3.0.4版,因此它完全是最新的.在github上,它甚至说要求应该使这项工作正常:https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility

最佳答案 看这篇文章:
https://github.com/mislav/will_paginate/issues/263

因此,对于您的控制器,它看起来像这样

def roster_builder
    require 'will_paginate/array'
  authorize! :access_roster_builder, current_coach
  @search = Search.new(sport: current_coach.primary_sport, conditions: params[:search])

  # @athletes = @search.query.page(params[:page]).per_page(8)
    @athletes = @search.query.paginate page: params[:page], per_page: 8

  if params[:refresh]
    render "athletes_refresh" 
  else
    render "athletes" if request.format.js?
  end
end
点赞