Will_paginate with multiple models

If you need to implement paging in your Ruby on Rails application, a quick and easy way is to use the fantastic “Will_Paginate” plugin (available here).

However I came across a need to paginate multiple models on the same page, for example; a list of users and a list of groups.

Luckily Google came to the rescue with this article, which demonstrates the use of the :page option, allowing you to specify the page against a model object.

Check out the code below (courtesay of candidcode.com)

Controller

@users = User.paginate(:page => params[:user_page], :per_page => 10)
@administrators = Administrator.paginate(:page => params[:administrator_page], :per_page => 10)

View

<%= will_paginate @users, :param_name => 'user_page' %>
<%= will_paginate @administrators, :param_name => 'administrator_page' %>


Leave a Reply

You must be logged in to post a comment.