kirqe
kirqe

Reputation: 2470

How to use will_paginate with model caching in rails?

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.

Upvotes: 3

Views: 580

Answers (1)

evanbikes
evanbikes

Reputation: 4171

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.

Upvotes: 3

Related Questions