thoughtbot / administrate

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

Field::Polymorphic in form causing form page to take very long to load #2004

Open arvinaik opened 3 years ago

arvinaik commented 3 years ago

I have a model Review which has a Field::Polymorphic Reviewable.

When I try to access the form page through new/edit, it takes very long to load due to the form loading every instance of a reviewable object.

Is there anyway to prevent the loading of all instance of the polymorphic class?

Alternatively is there a way I can have 'helper attributes' that do not exist in the model, but are used to determine the id of the polymorphic field?

pablobm commented 3 years ago

At the moment, Administrate doesn't support anything like that at the moment. I was playing with this today a bit, and saw a few problems that make this not trivial. For one, how would it work? If for example you'd want the options to load dynamically via Ajax, with a search string, there's the problem of how to filter across the different tables of the polymorphic associations, which will have different columns.

Something that you could try is to create a custom field, perhaps derived from the existing polymorphic one, which would then behave the way you specifically want.

As for the helper attributes, I'm not sure I understand. If you mean "virtual attributes", then you can have a field based off a method in your model. The example app does this with Customer#lifetime_value:

https://github.com/thoughtbot/administrate/blob/e1baea3e63cdec65774f1b0084ced5c4138f99a0/spec/example_app/app/models/customer.rb#L19-L21

However this will be read-only initially. Being able to write to it will require that you customize your controller to handle this param when the form is submitted.