简体   繁体   English

如何通过关联在使用 has_many 的模型上使用 ActiveAdmin?

[英]How to use ActiveAdmin on models using has_many through association?

I am using ActiveAdmin gem in my project.我在我的项目中使用ActiveAdmin gem。

I have 2 models using has_many through association.我有 2 个模型通过关联使用 has_many。 The database schema looks exactly the same as the example in RailsGuide.数据库模式看起来与 RailsGuide 中的示例完全相同。 http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association has_man 通过关联
(source: rubyonrails.org ) (来源: rubyonrails.org

How can I use ActiveAdmin to...如何使用 ActiveAdmin...

  1. show appointment date of each patient in physicians page?在医生页面中显示每位患者的预约日期?
  2. edit appointment date of each patient in physicians page?在医生页面编辑每位患者的预约日期?

Thanks all.谢谢大家。 :) :)

For 1)对于 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)对于 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:在回答tomblomfield的评论中跟进问题:

Try the following in your AA ActiveAdmin.register Model do block:在您的 AA ActiveAdmin.register Model 中尝试以下操作:

  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.它应该解决 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

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 @monfresh @tomblomfield 你可以做

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

in the physicians model在医师 model

...or, I'm not sure if you can use it with formtastic but you could make the scope optional with something like ...或者,我不确定你是否可以将它与 formtastic 一起使用,但你可以使用类似的东西使 scope 成为可选

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

and appointment.patient wont n+1 anymoreappointment.patient 。病人不会 n+1 了

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:要将其放入您的 redit 表单中,请先将 Appointment_ids 放入允许列表:

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:关于#2,它应该是这样的:

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)关于标题: - 它可能是错误的或一些 label(字符串)

Regarding the allow_destroy: - you can set it check for the user Administrator privilege's as can seen here关于allow_destroy: -您可以设置它检查用户管理员权限,如此处所示

Important - In the Physician model, make sure to have重要- 在医师 model 中,确保有

accepts_nested_attributes_for :appointments, allow_destroy: true

And, in the active admin model file - admin\physicians.rb - set this:并且,在活动管理员 model 文件中 - admin\physicians.rb - 设置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM