Paginated Squirrel

Jon Yurek

Latest and greatest…

001 >> posts = Post.find(:all) do
         updated_on > 1.year.ago
         paginate :page => 4, :per_page => 10
       end
    => [...]
005 >> posts.total_results
    => 46
006 >> posts.pages.current
    => 4
007 >> posts.pages.current_range
    => 31..40
008 >> posts.pages.next
    => 5
009 >> posts.pages.previous
    => 3

The new paginate squirrel method takes a hash with :page (1-indexed) and :per_page (which default to 1 and 20, respectively). When you use it, your results array is automatically extended with a helpful pages method that gives you all you need to know to paginate your views. It also puts a total_results method on there, so you know how big the whole search is.

And before you ask, yes, I know some of you asked for this to work with paginating_find, and it does. But we found that paginating_find was conflicting with other plugins, leading to some really weird errors. Plus, it’d always been my intention to add pagination (among other things) anyway.

Please Note: The repo has changed. It’s now on github.

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.