Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin: How to handle large associations

I'm building an interface for administrating organizations that can have lots of people connected. The total pool of people consists of a few thousand individuals.

To the best of my knowledge, AA doesn't really have a good system for this kind of situation.

So far I've used something like this in the form block to add/remove people from the organization:

f.has_many :person_organizations, for: [:person_organizations, f.object.person_organizations.active] do |connection_f|

  all_people = Person.select([:id, :firstname, :lastname]).order(:firstname, :lastname)

  connection_f.input :person, as: :select,
                              collection: all_people,
                              member_label: proc { |d| "#{d.firstname} #{d.lastname}"
  unless connection_f.object.nil?
    # Show the destroy checkbox only if it is an existing person
    # else, there's already dynamic JS to add / remove new dentists
    connection_f.input :_destroy, as: :boolean, label: 'Delete this connection'
  end
end

The problem with this is that after adding a few people to an organization, the time it takes to generate all the select boxes begins to be substantial as it has to do pretty much the same work for each element. See beneath ("slett denne koblingen" means "delete this connection")

Organization with some people added

Does anyone know of a way to alleviate this pain?

I've had a couple of thoughts, but I don't quite understand how I would implement them:

  • Only show a text string with the person's name after the association is set up, in stead of a select box. Need to still be able to delete the association, and create new ones, though.
  • Somehow cache the generated select box. Might give AA some problem with select-ing the correct value?

Also, I'm aware of a github issue discussing a solution to this kind of challenge, but it seems like it's still a way off, if it will ever be implemented: https://github.com/activeadmin/activeadmin/issues/2692#issuecomment-71500513

like image 393
rogerkk Avatar asked Nov 10 '22 19:11

rogerkk


1 Answers

I have encountered the same sluggishness. The challenge you are faced with is the number of populated selects that will get created. I recommend using some form of AJAX and Select2/Chosen combination to "autocomplete" a textbox input. This will greatly reduce the HTML footprint and speed load times. The user experience may be more appreciated by your admins too.

https://github.com/activeadmin/activeadmin/issues/1754

https://github.com/mfairburn/activeadmin-select2

Viget Labs has a gem that provides an alternate solution for handling the autocomplete association. https://github.com/vigetlabs/active_admin_associations

like image 192
scarver2 Avatar answered Nov 15 '22 06:11

scarver2