ActiveAdmin自动加载完整的关联表 [英] ActiveAdmin automatically loading full association table

查看:81
本文介绍了ActiveAdmin自动加载完整的关联表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用ActiveAdmin进行管理后端的项目。

I'm working on a project that uses ActiveAdmin for its administration backend.

我有两个模型,一个Book模型有很多产品。当我尝试访问ActiveAdmin中的产品索引视图时,似乎试图将完整的books表加载到内存中(我的数据库中大约有150万本书)。 CPU使用率上升到100%,内存使用率飙升至千兆字节。

I have two models, a Book model which has_many Products. When I try to access the products index view in ActiveAdmin, it seems to try to load the full books table into memory (there are about 1.5 million books in my database). CPU usage goes up to 100% and memory usage spikes to gigabytes.

打开mysql日志记录可确认在调用此视图时会发生这种情况:

Turning on mysql logging confirms that this is what happens when this view is called:

17 Query     SELECT `books`.* FROM `books`

据我所知,这是在尝试加载产品之前发生的。

As far as I can tell this happens before any attempt to load the products.

要弄清楚这个问题,我剥离了模型精打细算:

To figure out this issue I stripped the models down to their bare bones:

class Product < ActiveRecord::Base
  belongs_to :book
end

class Book < ActiveRecord::Base
  has_many :products
end

我还降低了AA定义为其最基本的形式:

I also reduced the AA definition to its most basic form:

ActiveAdmin.register Product do
end

ActiveAdmin是否正常?

Is this normal for ActiveAdmin? It doesn't seem like desirable behavior.

推荐答案

对于处理相同问题的任何人,我最终都将其追溯到自动生成的ActiveAdmin中的侧边栏。这包括一个搜索字段,其中包括一个用于所有关联记录的选择框。

For anyone dealing with this same issue, I finally traced it to the automatically generated sidebar in ActiveAdmin. This includes a search field that includes a select box for all associated records.

如果您有一个关联表超过一百万条记录,例如我这样做,AA会很乐意尝试插入

If you have an associated table with over a million records like I do AA will happily attempt to insert the entire table into the select box.

答案是在AA定义中为此类产品包括一些自定义过滤器:

The answer was to include some custom filters in the AA definition for products like so:

ActiveAdmin.register Product do
  filter :title
end

那样,将不包括该关联(除非您自己指定关联。)

That way the association won't be included (unless you specify it yourself.)

这篇关于ActiveAdmin自动加载完整的关联表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆