[Rails] paginate with will_paginate plugin


I'm using the will_paginate plugin for rails.
Unluckily, my query aren't in the form of:

MyModel.find(:all..)

in this case the main change is simply replacing 'find' with 'paginate' as described in the site.
I have a collection of MyModel object retrieved in a lot of different ways.
My solution, based on what i can see here , is calling a method (outside the controller class):

def self.paginate(my_collection, params)
current_page =
if params[:page].nil?
1
else
params[:page].to_i
end
per_page = 10
page_results = WillPaginate::Collection.create(current_page, per_page, my_collection.size) do |pager|
start = (current_page-1)*per_page
pager.replace(my_collection[start, per_page])
end
page_results
end

and, in the view:

<%= will_paginate @page_results, :params => params %>

Nessun commento: