Mongoid pagination

Still using will_paginate is also okay.

This thread has same issue: undefined method `paginate' for Array on Rails 3 with mongoid

The main point to fix the error is to add this line before the controller call paginate library:

require 'will_paginate/array'

It should be added to default config file if you use mongoid for the whole project.

Hope the explanation helpful.

Reference from origin gem source: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility at "WillPaginate::Collection" section.

P/S: this is just a work around if your query is not very large. If you want better performance, let's try mongoid-pagination gem, custom will_pagination gem or another pagination gem which supported Mongoid like kaminari.


This works fine for me:

@posts = Post.paginate(:page => 1, :limit => 10).desc(:_id)

desc(:_id) is added so that latest posts could be listed first.


You should use Kaminari https://github.com/amatsuda/kaminari


A bit late, but for anyone else looking, I found 'will_paginate_mongoid'

https://github.com/lucasas/will_paginate_mongoid

Really straight forward and lets you simply do

collection.skip(20).limit(10)