【问题标题】:How to use will_paginate with model caching in rails?如何在 Rails 中使用 will_paginate 和模型缓存?
【发布时间】:2015-01-08 07:58:55
【问题描述】:

我的模型中有一个范围 (scope :short_listed, -> ....)。要列出索引页面上的所有项目,我使用以下代码:

@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

我有这个错误

undefined method `paginate' for #<Array:

如果我删除 .paginate(:page =&gt; params[:page])&lt;%= will_paginate....%&gt; 一切正常。

如何使 will_paginate 与模型缓存一起使用。

【问题讨论】:

    标签: ruby-on-rails caching


    【解决方案1】:

    WillPaginate 默认不包含数组分页,但你可以包含它...

    require 'will_paginate/array'
    

    ...在初始化程序中。数组上的分页应该与 ActiveRecord::Relation 上的分页几乎完全相同。

    猜你喜欢
    • 2010-10-03
    • 2011-04-21
    • 2014-05-07
    • 2010-11-08
    • 2023-03-18
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多