How to use ActiveAdmin on models using has_many through association?

前端 未结 6 1258
南笙
南笙 2020-12-12 11:28

I am using ActiveAdmin gem in my project.

I have 2 models using has_many through association. The database schema looks exactly the same as the example in RailsGuide

相关标签:
6条回答
  • 2020-12-12 11:57

    It's work for me (with chosen)

    permit_params category_ids: []
    
    form do |f|
       inputs 'Shop' do
         input :category_ids, collection: Category.all.collect {|x| [x.name, x.id]}, as: :select, multiple: true, input_html: { class: "chosen-input",  style: "width: 700px;"}
        end
       f.actions
    end
    
    0 讨论(0)
  • 2020-12-12 11:58

    For 1)

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

    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
      end
    end
    
    0 讨论(0)
  • 2020-12-12 12:06

    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

    0 讨论(0)
  • 2020-12-12 12:08

    If you would like show multiple field in a panel row you can use following view:

    show do |phy|
       panel "Details" do
            attributes_table do
              ... # Other fields come here
              row :appointment_dates do
                apps=""
                phy.appointments.all.each do |app|
                  apps += app.patient.name + ":" + app.appoinment_date + ", "
                end
                apps.chomp(", ")
              end
            end      
       end
    end
    

    To place it in you redit form first put appointment_ids to permitted list:

    permit_params: appointment_ids:[]
    

    Add has many relationship to the form

    form do |f|
       f.has_many :appointments do |app|
         app.inputs "Appointments" do
           app.input :patients, :as => :select, :label => "Assigned Patients"
           app.input :appointment_date
         end  
       end
    end
    

    Should work if there is no coding error.

    0 讨论(0)
  • 2020-12-12 12:11

    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
      end
    end
    
    0 讨论(0)
  • 2020-12-12 12:16

    @monfresh @tomblomfield you can do

    has_many :appointments, ->{ includes(:patients) }, :through => :patients
    

    in the physicians model

    ...or, I'm not sure if you can use it with formtastic but you could make the scope optional with something like

    has_many :appointments :through => :patients do
      def with_patients
        includes(:patients)
      end
    end
    

    and appointment.patient wont n+1 anymore

    0 讨论(0)
提交回复
热议问题