mongodb, mongoid, will_paginate and sorting
Posted: April 6th, 2010 | Author: jgeiger | Filed under: mongodb, ruby, web | Tags: mongodb, mongoid, ruby | 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.
Thanks, I needed this!
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
You can also just put it at the beginning.
order_by(:created_at.desc).paginate(:page => page, :per_page => size)
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!