How to use will_paginate with model caching in rails?

There is a region ( scope :short_listed, -> ....

) in my model . To list all the elements on the 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 uninstall .paginate(:page => params[:page])

and <%= will_paginate....%>

everything works fine.

How to make will_paginate work with model caching.

+3


source to share


1 answer


WillPaginate doesn't enable array paging by default, but you can enable it ...

require 'will_paginate/array'

      



... in the initializer. Array pagination should work in much the same way as pagination in ActiveRecord :: Relation.

+3


source







All Articles