I’m having a big problem with will paginate.
I have a query defined as scope in my model:
scope :published_and_valid, joins(...).where(...)
In my controller I sometimes just want to have the top 20.
.limit(20)
Now I want to be able to paginate over these 20 results (5 per page)
And I try to do this:
@articles = Article.published_and_valid.limit(20).paginate(:page => params[:page])
This does not work as the result in my view is a pagineted result for ALL articles
It looks like will_paginate imposes its own
limitclause on your relation in order to do the paging. After some experimentation, the easiest way I could find to do this is to do eager-loading of the result set and use the functionality contained in the array extensions to do the actual pagination.Here’s a quick example; note that you have to require
will_paginate/array; you may wish to do this in an initializer or somewhere else. Note that it does monkey-patchArrayto have thepaginatemethod.[Update]
Another option is to pass in the
total_entiresoption to will_paginate based on the result set size.You can’t pass in 20 as you can’t guarantee that a full 20 results will be returned; by passing in the
countattribute, you can ensure that the pagination will be correct.