3

I have a scope in my model(scope :short_listed, -> ....). To list all the items on index page I use this code:

@posts = Post.cached_short_listed.paginate(:page => params[:page])

post.rb

  def self.cached_short_listed
    Rails.cache.fetch([name, "short_listed"], expires_in: 5.minutes) do
      short_listed.to_a
    end
  end

and I have this error

undefined method `paginate' for #<Array:

if I remove .paginate(:page => params[:page]) and <%= will_paginate....%> everything works fine.

How to make will_paginate work with model caching.

kirqe
  • 2,431
  • 4
  • 37
  • 63

1 Answers1

3

WillPaginate doesn't include Array pagination by default, but you can include it...

require 'will_paginate/array'

...in an initializer. Pagination on an array should work almost exactly like pagination on an ActiveRecord::Relation.

evanbikes
  • 4,131
  • 1
  • 22
  • 17