Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LandonSchropp opened this issue May 22, 2024 · 5 comments
Open

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

LandonSchropp opened this issue May 22, 2024 · 5 comments

Comments

@LandonSchropp
Copy link

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
Copy link
Contributor

Nitr commented Aug 23, 2024

take a look at this

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

@LandonSchropp
Copy link
Author

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
Copy link
Contributor

Nitr commented Aug 23, 2024

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
  1. 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
Copy link
Member

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
Copy link

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 #2600, which seems related. A lazy-load flag to with_options or a separate field type IMO would be super straightforward user-interface-wise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants