administrate
administrate copied to clipboard
Field::HasMany loading too many records in Edit view
I'm building a dashboard for my Price model. One of the attributes on this model is a HasMany field.
class Monetization::PriceDashboard < Administrate::BaseDashboard
...
ATTRIBUTE_TYPES = {
...
companies: Field::HasMany,
}.freeze
FORM_ATTRIBUTES = %i[
...
companies
].freeze
end
This works fine locally, but in production, the input for the companies loads every record in the database. This is too much data, and the page crashes before it can finish rendering.
Is it possible to configure Field::HasMany to display a limited subset of the records for the edit drop-down? I'd expect the field to re-fetch the data when the user types in a more specific query.
I saw that, but I'm not sure if it solves my problem.
In our app, it really is possible to pick from any of the options—it's just the UI that's loading too many of them at once to pick from. If I were to limit them, then I'd exclude valid options to choose from.
An ideal solution for our use case would be a dropdown that lazy-loads its contents with a dashboard method that's capable of selecting a subset of our models based on a query string.
I saw that, but I'm not sure if it solves my problem.
In our app, it really is possible to pick from any of the options—it's just the UI that's loading too many of them at once to pick from. If I were to limit them, then I'd exclude valid options to choose from.
An ideal solution for our use case would be a dropdown that lazy-loads its contents with a dashboard method that's capable of selecting a subset of our models based on a query string.
In my experience,
- I created controller named SuggestionsController with only index action. Something like this...
class SuggestionsController < ActionController::API
def index
resources = authorized_scope(params[:resource_class].constantize.all).search(params.require(:q))
@page, @resources = pagy(resources)
end
end
- Then I had to override
_form.html.erbpartials for belongs_to, has_one.... for all associative fields in such a way all could work with select2-ajax-remote-data
One option would be to create a lazy-load HasMany field. This is kind of a rubbish solution, but would be easiest with the plugin hooks we already have.
A better solution would be to have a flag we could set on the existing one to lazy load the data, but to go about that, I think we'd want to think about the other cases where this is something we'd likely need to do.
Have others come across a similar situation where they need to load a lot of data into one field?
Have others come across a similar situation where they need to load a lot of data into one field?
Yes, but I haven't a solution quite yet. While searching for one I found this Issue and https://github.com/thoughtbot/administrate/issues/2600, which seems related. A lazy-load flag to with_options or a separate field type IMO would be super straightforward user-interface-wise.