Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin automatically loading full association table

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

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.

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

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

ActiveAdmin.register Product do
end

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

like image 942
Peter Duijnstee Avatar asked Aug 31 '13 20:08

Peter Duijnstee


2 Answers

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.

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.

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.)

like image 197
Peter Duijnstee Avatar answered Oct 30 '22 05:10

Peter Duijnstee


A better approach now is to use remove_filter for the particular attribute or relationship:

Or you can also remove a filter and still preserve the default filters:

preserve_default_filters!
remove_filter :id

https://activeadmin.info/3-index-pages.html

like image 42
Carson Reinke Avatar answered Oct 30 '22 06:10

Carson Reinke