thoughtbot / administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
http://administrate-demo.herokuapp.com
MIT License
5.9k stars 1.12k forks source link

Field::HasMany loading too many records in Edit view #2593

Open LandonSchropp opened 5 months ago

LandonSchropp commented 5 months ago

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.

Nitr commented 2 months ago

take a look at this

another solution is to define a scope in your model with limit: check tihs

LandonSchropp commented 2 months ago

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.

Nitr commented 2 months ago

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,

  1. 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
  2. Then I had to override _form.html.erb partials for belongs_to, has_one.... for all associative fields in such a way all could work with select2-ajax-remote-data
nickcharlton commented 1 month ago

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?

wTheRockb commented 1 month ago

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.