0

I am trying to paginate some items from my database however I am getting this error when I try to go onto page two of my pagination:

RangeError Exception: offset 25 and limit 25 are outside allowed range

This is the code in my controller that sets up the pagination:

@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

I limit the amount of objects returned in my query to 40, then try to paginate the result set however I am getting a range error. I am not sure what the problem is here. If I take the limit off my initial query this works fine.

This is the output in the terminal:

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'

Can anyone help please?

Hugs
  • 915
  • 4
  • 20
  • 47

1 Answers1

1

Not sure if this will help but you could try passing the total count in

@activities = Kaminari.paginate_array(@activities, total_count: 40 ).page(@page).per(10)

There are also offset and limit parameters you can call paginate_array with, which may help.

*UPDATE*

This may also lead you on the right path

https://github.com/amatsuda/kaminari/pull/160

https://github.com/amatsuda/kaminari/pull/111

Purple Hexagon
  • 3,538
  • 2
  • 24
  • 45
  • Thanks for the attempt but when I do that, more than 10 elements show up in the pagination and when I go to page 2, the same items are there. – Hugs Oct 29 '12 at 16:52