我正在尝试从我的数据库中分页一些项目,但是当我尝试进入我的分页的第二页时,我收到此错误:
RangeError Exception: offset 25 and limit 25 are outside allowed range
这是我的控制器中设置分页的代码:
@activities = (current_user.followed_users.activities(:order => [:created_at.desc]) + current_user.followed_centers.activities(:order => [:created_at.desc])).all(:limit => 40)
unless (params[:page].nil?)
@page = params[:page].to_i
end
unless (@page.nil?)
@activities = Kaminari.paginate_array(@activities).page(@page).per(10)
else
@activities = Kaminari.paginate_array(@activities).page(0).per(10)
end
我将查询中返回的对象数量限制为40,然后尝试对结果集进行分页,但是我收到范围错误。我不知道这里有什么问题。如果我从初始查询中取消限制,这可以正常工作。
这是终端的输出:
RangeError (offset 25 and limit 25 are outside allowed range):
lib/ct_gems/dm-core-1.2.0/lib/dm-core/query.rb:1298:in `get_relative_position'
lib/ct_gems/dm-core-1.2.0/lib/dm-core/query.rb:592:in `slice!'
lib/ct_gems/dm-core-1.2.0/lib/dm-core/query.rb:567:in `slice'
lib/ct_gems/dm-core-1.2.0/lib/dm-core/collection.rb:1414:in `sliced_query'
lib/ct_gems/dm-core-1.2.0/lib/dm-core/collection.rb:396:in `[]'
app/controllers/users_controller.rb:64:in `stream'
lib/ct_gems/dm-core-1.2.0/lib/dm-core.rb:263:in `block in repository'
lib/ct_gems/dm-core-1.2.0/lib/dm-core/repository.rb:114:in `scope'
lib/ct_gems/dm-core-1.2.0/lib/dm-core.rb:263:in `repository'
有人可以帮忙吗?
答案 0 :(得分:1)
不确定这是否有帮助,但您可以尝试传递
中的总计数@activities = Kaminari.paginate_array(@activities, total_count: 40 ).page(@page).per(10)
还可以调用paginate_array的偏移和限制参数,这可能有所帮助。
*的 更新 * 强>
这也可能引导你走上正确的道路