How to use ActiveAdmin on models using has_many through association? How to use ActiveAdmin on models using has_many through association? ruby-on-rails ruby-on-rails

How to use ActiveAdmin on models using has_many through association?


For 1)

show do  panel "Patients" do    table_for physician.appointments do      column "name" do |appointment|        appointment.patient.name      end      column :appointment_date    end  endend

For 2)

form do |f|  f.inputs "Details" do # physician's fields    f.input :name  end  f.has_many :appointments do |app_f|    app_f.inputs "Appointments" do      if !app_f.object.nil?        # show the destroy checkbox only if it is an existing appointment        # else, there's already dynamic JS to add / remove new appointments        app_f.input :_destroy, :as => :boolean, :label => "Destroy?"      end      app_f.input :patient # it should automatically generate a drop-down select to choose from your existing patients      app_f.input :appointment_date    end  endend


In answer tomblomfield follow up question in comments:

Try the following in your AA ActiveAdmin.register Model do block:

  controller do    def scoped_collection      YourModel.includes(:add_your_includes_here)    end  end

This should lazy load all your associations for each index page in a separate query

HTH


It should solve the N+1 query problem.

show do  panel "Patients" do    patients = physician.patients.includes(:appointments)    table_for patients do      column :name      column :appointment_date { |patient|    patient.appointments.first.appointment_date }    end  endend