mongodb, mongoid, will_paginate and sorting

Posted: April 6th, 2010 | Author: | Filed under: mongodb, ruby, web | Tags: , , | 4 Comments »

Had a bit of an issue with will_paginate and mongoid. I couldn’t find an example of how to sort the pagination query and paginating without a defined sort order defeats the purpose.

paginate(:page => page, :per_page => size, :sort => [['ontology_term_id', :desc], ['_id', :asc]])

Instead of using “order” or “order_by” you can just use “sort” with an array of arrays.


4 Comments on “mongodb, mongoid, will_paginate and sorting”

  1. 1 Daniel Kehoe said at 7:07 pm on July 5th, 2010:

    Thanks, I needed this!

  2. 2 ljay said at 10:55 pm on October 27th, 2010:

    Hi,

    I’m struggling to paginate using mongoid. I’m able to call paginate on my collection object like

    @results = @collections.paginate :page => 1, :per_page => 10

    but how it needs to be referred in view?

    In my view I have rendered the collection in partial as
    @collections, :partial => collect.xml %>

    In the above line I get the error undefined method total_pages for array.

    But there is no error if I remove the will_paginate call in view and all the collections are shown in the view without pagination. Pls help

  3. 3 stellard said at 5:32 pm on February 2nd, 2011:

    You can also just put it at the beginning.

    order_by(:created_at.desc).paginate(:page => page, :per_page => size)

  4. 4 Victor said at 1:30 pm on March 6th, 2011:

    Thanks for this post as well, to sellard:

    You can also just put it at the beginning.
    order_by(:created_at.desc).paginate(:page => page, :per_page => size)

    nice!


Leave a Reply