17

I tried

@posts = Post.page(params[:page]).per_page(10)

and

@posts = Post.paginate(:page => 1, :per_page => 10)    

but neither method works

undefined method `page' for Post:Class

undefined method `paginate' for Post:Class

How do you do pagination with mongoid?

Leopd
  • 41,333
  • 31
  • 129
  • 167
Vyacheslav Loginov
  • 3,136
  • 5
  • 33
  • 49
  • Just using some library to pagination with MongoDB. You can use: gem 'will_paginate' and gem "will_paginate_mongoid" – ThienSuBS Feb 01 '18 at 02:45

8 Answers8

13

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

Vyacheslav Loginov
  • 3,136
  • 5
  • 33
  • 49
5

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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
vedmar
  • 59
  • 1
  • 3
5

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.

Community
  • 1
  • 1
Hoang Le
  • 1,341
  • 14
  • 14
  • I think origin version of will_paginate didn't support no-sql databases like Mongoid so calling the base core of this gem will resolve this issue by consider the database record like normal arrays, not special sql database format like MySQL, SQLite, ... – Hoang Le Mar 27 '14 at 09:57
  • This should be the correct answer and not recommending another gem. – Júlio Campos Feb 03 '20 at 11:24
3

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)
Doobi
  • 4,844
  • 1
  • 22
  • 17
  • If for some reason you need to use `will_paginate` and you run `rails-4`, `will_paginate_mongoid` din't work last time I checked. The solution is to place [mongoid_paginator.rb](https://raw.github.com/lucasas/will_paginate_mongoid/master/lib/will_paginate_mongoid/mongoid_paginator.rb) in `app/models/concerns`, remove the outer `module WillPaginateMongoid` and then `include MongoidPaginator` in your model class. – yanychar Jan 19 '14 at 11:06
  • will_paginate_mongoid doesn't look like it is actively maintained anymore. That last commit on the repo was `Sep 12, 2013`. – Robert Reiz Jun 14 '20 at 13:47
2

Use the following gem.

Very helpful.

https://github.com/lucasas/will_paginate_mongoid

Pradeep
  • 416
  • 5
  • 9
2

To update a bit the answers, now exists the Pagy gem, also much more performant (cpu/mem) than will_paginate and kaminari. This is a migration guide

MegaTux
  • 1,591
  • 21
  • 26
1

silly thing, but it worked for me in sinatra after i added require 'mongoid-pagination' to app.rb

Dvid Silva
  • 1,110
  • 13
  • 25
1

Posting several years later, in case someone else faces the same challenge.

kaminari-mongoid was released in 2016, and is currently maintained.

https://github.com/kaminari/kaminari-mongoid

All the goodness of Kaminiari for Mongoid, including appropriate handling of Mongoid::Criteria results, which was possibly the cause of the OP's error.

changingrainbows
  • 2,551
  • 1
  • 28
  • 35
  • Does that work with Mongoid 7.X? It doesn't work for me. Always getting back `undefined method page`. – Robert Reiz Jun 14 '20 at 13:48
  • I would migrate to Pagy gem. Kaminari performance & mem. usage is even worse than will_paginate_mongoid, [see here](https://ddnexus.github.io/pagination-comparison/gems.html) – MegaTux Dec 10 '20 at 13:55