简体   繁体   English

如何在rails中使用带模型缓存的will_paginate?

[英]How to use will_paginate with model caching in rails?

I have a scope in my model( scope :short_listed, -> .... ). 我的模型中有一个范围( 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 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. 如果我删除.paginate(:page => params[:page])<%= will_paginate....%>一切正常。

How to make will_paginate work with model caching. 如何使will_paginate与模型缓存一起工作。

WillPaginate doesn't include Array pagination by default, but you can include it... 默认情况下,WillPaginate不包括Array分页,但您可以包含它...

require 'will_paginate/array'

...in an initializer. ...在初始化程序中。 Pagination on an array should work almost exactly like pagination on an ActiveRecord::Relation. 数组上的分页应该几乎完全像ActiveRecord :: Relation上的分页一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM