我的模型中有一系列来自Active Record的集合。
@ad_groups = AdGroup.where(is_template: false).paginate(:page => params[:page]).order(:id).reverse
然后在我的模型中我有
self.per_page = 25
WillPaginate.per_page = 25
在我的索引视图
中= will_paginate @ad_groups
还需要will_paginate / array。我看到有人在初始化程序中使用了这段代码
require 'will_paginate/collection'
class Array
# Paginates a static array (extracting a subset of it). The result is a
# WillPaginate::Collection instance, which is an array with a few more
# properties about its paginated state.
#
# Parameters:
# * <tt>:page</tt> - current page, defaults to 1
# * <tt>:per_page</tt> - limit of items per page, defaults to 30
# * <tt>:total_entries</tt> - total number of items in the array, defaults to
# <tt>array.length</tt> (obviously)
#
# Example:
# arr = ['a', 'b', 'c', 'd', 'e']
# paged = arr.paginate(:per_page => 2) #-> ['a', 'b']
# paged.total_entries #-> 5
# arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd']
# arr.paginate(:page => 3, :per_page => 2) #-> ['e']
#
# This method was originally {suggested by Desi
# McAdam}[http://www.desimcadam.com/archives/8] and later proved to be the
# most useful method of will_paginate library.
def paginate(options = {})
page = options[:page] || 1
per_page = options[:per_page] || WillPaginate.per_page
total = options[:total_entries] || self.length
WillPaginate::Collection.create(page, per_page, total) do |pager|
pager.replace self[pager.offset, pager.per_page].to_a
end
end
end
没用。知道我做错了什么吗?
答案 0 :(得分:2)
那是因为您在查询结束时有.reverse
。此方法将返回模型数组,而不是ActiveRecord::Relation
。只需添加.order()
方法所需的订单:
@ad_groups = AdGroup.where(is_template: false)
.paginate(:page => params[:page]).order('id DESC')
# HERE ======= |