【问题标题】:Rails - Active Admin paginating the index pageRails - 对索引页面进行分页的活动管理员
【发布时间】:2013-12-28 15:45:13
【问题描述】:

我的应用程序中 Article 模型的 Active Admin 索引页面需要很长时间才能加载。这是因为有超过 2500 个文章条目,并且因为我启用了过滤器(并且我需要启用过滤器)。我觉得我已经在做文档推荐的所有事情,当谈到加快速度时,但不知何故,它仍然在拉起每篇文章(当我只希望它拉起特定页面上的文章时)。也就是说,分页似乎无法正常工作。

以下是代码示例:

ActiveAdmin.register Article do
    filter :title
    config.sort_order = "published_desc"
    config.per_page = 10

    index :pagination_total => false do |article|
        # code for my columns goes here
    end
end

以下是日志中的一些相关输出。有很多很多这样的调用,即使我只是尝试按标题搜索一篇文章(例如使用过滤器):

  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (258.8489ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (234.8890ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (269.1431ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (240.3183ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (250.5591ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (403.0311ms)
  MOPED: 127.0.0.1:27017 GET_MORE     database=[db_name] collection=articles limit=0 cursor_id=1211541689548427701 (250.7601ms)

【问题讨论】:

    标签: ruby-on-rails filter pagination activeadmin


    【解决方案1】:

    删除特定过滤器通常有助于加快索引视图的速度。问题是 ActiveAdmin 将所有资源的关系加载为过滤器选项,其中许多可能不是必需的。

    删除未使用的过滤器

    可以使用remove_filter 方法单独删除过滤器。

    ActiveAdmin.register Article do
      remove_filter :category
    end
    

    转换为字符串过滤器

    另一种方法是更改​​过滤器的类型。很多时候,选择不需要按相关记录进行过滤。您可以改为将其定义为基于字符串的过滤器。

    ActiveAdmin.register Article do
       # filter :category # <= Slow due to number of categories
       filter :category_name, as: :string
    end
    

    自定义过滤器选项

    还可以自定义可用的选项,或仅提供选项的子集。

    ActiveAdmin.register Article do
       filter :category, collection: proc { Category.limit(10) }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多