它看起来像will_paginate强加给你的关系了自己limit
条款,以做寻呼pagineted结果。经过一番实验后,我能找到的最简单的方法就是尽快加载结果集,并使用the array extensions中包含的功能进行实际的分页。
下面是一个简单的例子;请注意,您必须要求will_paginate/array
;你可能希望在初始化程序或其他地方执行此操作。请注意,它有monkey-patch Array
以拥有paginate
方法。
require 'will_paginate/array'
class ArticlesController < ApplicationController
def index
@articles = Article.published.limit(20).all # call `all` to eager load
@articles = @articles.paginate(:page => params[:page], :per_page => 10)
end
end
[更新]
另一种选择是在total_entires
选项传递给will_paginate基于所述结果集的大小。
class ArticlesController < ApplicationController
def index
@articles = Article.published.limit(20)
@articles = @articles.paginate(:page => params[:page], :per_page => 10, :total_entries => @articles.count)
end
end
您无法传入20,因为您无法保证将返回完整的20个结果;通过传递count
属性,可以确保分页是正确的。
你安装了什么版本的will_paginate? (如果有疑问,请检查你的'Gemfile.lock')。 –
版本是3.0.2 – hereandnow78