Search code examples
ruby-on-railsruby-on-rails-3rubygemsassociationsactiveadmin

How to use ActiveAdmin on models using has_many through association?


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. http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association has_many through association
(source: rubyonrails.org)

How can I use ActiveAdmin to ...

  1. show appointment date of each patient in physicians page?
  2. edit appointment date of each patient in physicians page?

Thanks all. :)


Solution

  • 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