I am currently trying to speed up an old (3.2) Rails app and hitting an issue with way too many calls being made to the database during an edit view using simple_form
Here is the simplified setup;
class Event
HABTM Speakers
HABTM Sponsors
class Speaker
belongs_to Sponsor
HABTM Events
class Sponsor
have_many Speakers
HABTM Events
For an event we want to show both each potential speaker (of which there are thousands) and all the sponsors (hundreds).
For each speaker if they have a sponsor they need to show the sponsor name.
This all adds up to a lot of calls to the sponsors
table, way too many.
Current Code
Right now the simple_form is set up as;
<%= simple_form_for [:admin, @event] do |f| %>
...
<%= f.association :speakers, :input_html => { :class => 'select2able' }, :label_method => :summary %>
...
<%= f.association :sponsors, :input_html => { :class => 'select2able' } %>
...
That label method is on the speaker
model;
delegate :name, to: :sponsor, prefix: true, allow_nil: true
def summary
[name, job_title, sponsor_name].compact.join ' - '
end
The @event variable is assigned in the controller with a simple
@event = Event.find params['id']
This is where I have been spending most of my time trying to improve.
What I have tried
Calling Event.includes(:speakers => :sponsor).find()
I thought would help. but it hasn't at all.
I have also tried just including speaker and sponsor separately.
I have searched around for people having issues with simple_form
specifically but haven't found anything. I know this is a sign of the mental structure that was made, but anything I can do to improve it without a complete re-write would be excellent.