How to use ActiveAdmin for models using has_many through union?

I am using ActiveAdmin gem in my project.

I have 2 models using has_many through association. The database schema looks exactly like the example in RailsGuide. http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association has_many through the association http://guides.rubyonrails.org/images/has_many_through.png

How to use ActiveAdmin to ...

  • show the appointment date of each patient on the doctors page.
  • Change the appointment date of each patient on the doctors page.

Thanks to everyone. :)

+47
ruby-on-rails ruby-on-rails-3 associations gem activeadmin
Aug 17 '11 at 20:30
source share
6 answers

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 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 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 
+70
Aug 18 '11 at 2:45 a.m.
source share
— -

In response to the Ombudsman’s question in the comments:

Try the following steps in the do block of the AD ActiveAdmin.register block:

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

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

NTN

+13
Jan 27 2018-12-12T00:
source share

It should solve the problem with query N + 1.

 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 
+1
Jun 19 '15 at 14:04
source share

It works for me (with selected)

 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 
+1
Mar 03 '17 at 14:20
source share

@monfresh @tomblomfield you can do

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

in the model of doctors

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

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

and appointment.patient will no longer be + 1

0
Jan 19 '15 at 22:56
source share

If you want to show several fields in a panel bar, you can use the 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 the selected form, first set the destination_ids parameter to the allowed list:

 permit_params: appointment_ids:[] 

Add has a lot to do with 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
Aug 26 '16 at 22:17
source share



All Articles