Mongoid pagination

后端 未结 8 578
無奈伤痛
無奈伤痛 2021-02-05 12:01

I tried

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

and

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


        
相关标签:
8条回答
  • 2021-02-05 12:37

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

    0 讨论(0)
  • 2021-02-05 12:38

    Use the following gem.

    Very helpful.

    https://github.com/lucasas/will_paginate_mongoid

    0 讨论(0)
  • 2021-02-05 12:39

    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.

    0 讨论(0)
  • 2021-02-05 12:42

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

    0 讨论(0)
  • 2021-02-05 12:44

    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

    0 讨论(0)
  • 2021-02-05 12:45

    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.

    0 讨论(0)
提交回复
热议问题