0

I am using Ruby on Rails 3.2.9 and the WillPaginate 3.0 ruby gem. As pointed out by many resources on Internet (keep reading for more information), it seems that something doesn't work as expected on counting objects with the WillPaginate gem when "complex" associations / scoping methods are involved, since this issue makes it to generate a wrong amount of page links in related views. In my case the problem is similar to this and this.

In my model class Article I have:

has_many :category_associations
has_many :categories, :through => :category_associations

In the related controller ArticleControllers I have :

@article
  .categories
  .scope_method(...)
  .order_method
  .search_method(...)
  .paginate(:page => 1, :per_page => 10)

Proposed solutions on the Internet are:

Article 1: The solution is to add a count condition this way:

@article =
  <...> # Same as above.
  .paginate(:per_page => 4, :page => params[page], :count => {:group => 'articles.id'})

Article 2: The solution is to manually count objects and set the total_entries option this way:

# Note: The below code is performance less since a further SQL query is executed.

total_entries = ... # Pre-count the amount of objects, as needed (note: in this code line it is executed a SQL query to the database).

@article =
  <...> # Same as above.
  .paginate(:per_page => 4, :page => params[page], :total_entries => total_entries)

I tried and made to work all the above solutions, but I would like to understand what could be the "internal" issue related to the WillPaginate gem (so that I can adjust it) and how I should proceed to properly solve my problem (for example, what is the "best" solution? are there other solutions than the above ones?).

For instance, I would like to avoid to use "hacked" solutions like those presented above but to use the paginate method the "common" way: @articles.<...>.paginate(:per_page => 4, :page => params[page]).

Community
  • 1
  • 1
Backo
  • 18,291
  • 27
  • 103
  • 170
  • Could take a look at [ruby toolbox](https://www.ruby-toolbox.com/categories/pagination)'s other solutions. [Kaminari](https://github.com/amatsuda/kaminari) might be good. Or, it might benefit you to just use skip and take on `ActiveRecord::Relation` (which it delegates, so they aren't listed in the [apidoc](http://api.rubyonrails.org/classes/ActiveRecord/Relation.html)), e.g. `YourModel.scoped.skip(5).take(5)` or `YourModel.where('').skip(5).take(5)`. Similarly, counting can be done with `YourModel.scoped.count`, `YourModel.where('').count`. – Gary S. Weaver May 30 '13 at 14:52
  • Thank you for pointing out to the Kaminari gem. I know that. However, before of now, I've never had trouble using WillPaginate so I would like to keep using that gem (for now). Why you say me to use the Kaminari gem? There is some relevant reason for using that gem? – Backo May 30 '13 at 14:55
  • If having scope, etc. complications, it might help. Kaminari reduces the amount you have to do manually with AR Relation and adds some helpful methods. As it says on its page, "No special collection class or anything for the paginated values, instead using a general AR::Relation instance. So, of course you can chain any other conditions before or after the paginator scope." I don't have any personal problems with WillPaginate, though, and it is well-used. – Gary S. Weaver May 30 '13 at 15:02
  • 1
    Also, there are serious security issues with Rails 3.2.9. You should update to the latest asap. – Gary S. Weaver May 30 '13 at 15:06

0 Answers0