简体   繁体   中英

Rails - Active Admin paginating the index page

The Active Admin index page for the Article model in my application takes a very long time to load. This is because there are >2500 article entries, and because I have filters enabled (and I need filters enabled). I feel like I'm already doing everything that the documentation recommends, when it comes to speeding things up, but somehow it's still pulling up every article (when I only want it to pull up the articles on the specific page). That is to say, it seems like pagination is not working properly.

Here's a sampling of the code:

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

And here's some relevant output from the log. There are many, many calls of this sort, even when I simply try to search for a single article by title (eg using the filter):

  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)

Removing specific filters often helps to speed up index views. The trouble is ActiveAdmin loads all of the resource's relationships as filter options, many of which might not be required.

Remove Unused Filters

Filters can be removed individually using the remove_filter method.

ActiveAdmin.register Article do
  remove_filter :category
end

Convert to String Filters

Another approach is to change the filter's type. Many times a select isn't necessary to filter by a related record. You could instead define it as a string based filter.

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

Customize Filter Options

It is also possible to customize what options are available, or only provide a subset of options.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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