Read more

will_paginate can paginate plain Ruby arrays

Henning Koch
January 03, 2013Software engineer at makandra GmbH

While you are probably using will_paginate Show archive.org snapshot to paginate ActiveRecord scopes, it can actually paginate plain Ruby arrays. The resulting arrayish object will have the same methods as a paginated scope, e.g. #total_entries. This means you can render pagination controls with the same code that works with paginated scopes.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

To enable this, add an initializer config/initializers/array_paginate.rb:

require 'will_paginate/array'

You can now say:

> numbers = (1..1000).to_a
> page = numbers.paginate(:page => 2, :per_page => 10)
=> [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 
> page.total_entries
=> 1000
Posted by Henning Koch to makandra dev (2013-01-03 18:43)