English 中文(简体)
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. :)

最佳回答

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
问题回答

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
  end
end

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

@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

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.

Regarding #2, it should be like this:

form do |f|
  f.inputs  Physician Details  do
    f.input :name
  end

  f.inputs  Physician Appointments  do
    f.has_many :appointments,
               heading: false,
               new_record:  Add new appointment ,
               remove_record:  Delete appointment ,
               allow_destroy: true do |app|
    app.input :patient, label:  Choose the patient , collection: Patient.pluck(:name, :id)
    app.input :appointment_date
  end
end

Regarding the heading: - it can be false or some label (string)

Regarding the allow_destroy: - you can set it check for the user Administrator privilege s as can seen here

Important - In the Physician model, make sure to have

accepts_nested_attributes_for :appointments, allow_destroy: true

And, in the active admin model file - adminphysicians.rb - set this:

permit_params :name, appointments_attributes: [:patient_id, :_destroy, :id]




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签