---
title: "Paginate a collection or an array"
author: "koska"
date: "2007-02-05T13:31:53Z"
tags: []
slug: "paginate-a-collection-or-an-array"
draft: false
lastmod: "2007-02-05T13:31:53Z"
id: 52293
url: "https://bbx.iteye.com/blog/52293"
---

def paginate\_collection(collection, options = {})  
    default\_options = {:per\_page =&gt; 10, :page =&gt; 1}  
    options = default\_options.merge options  
    pages = Paginator.new self, collection.size, options\[:per\_page], options\[:page]  
    first = pages.current.offset  
    last = \[first + options\[:per\_page], collection.size].min  
    slice = collection\[first...last]  
    return \[pages, slice]  
end

The above snippet can be invoked using  
@pages, @users = paginate\_collection User.find\_custom\_query, :page =&gt; @params\[:page]