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

时间:2011-08-17 20:30:33

标签: ruby-on-rails ruby-on-rails-3 associations gem activeadmin

我在我的项目中使用ActiveAdmin gem。

我有2个模型使用has_many通过关联。数据库模式看起来与RailsGuide中的示例完全相同。 http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association has_many through association http://guides.rubyonrails.org/images/has_many_through.png

如何使用ActiveAdmin ...

  1. 在医生页面显示每位患者的预约日期?
  2. 在医生页面中编辑每位患者的预约日期?
  3. 谢谢大家。 :)

7 个答案:

答案 0 :(得分:75)

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

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

答案 1 :(得分:14)

在回答tomblomfield时,请在评论中跟进问题:

在AA ActiveAdmin.register Model do块中尝试以下操作:

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

这应该在单独的查询中延迟加载每个索引页的所有关联

HTH

答案 2 :(得分:1)

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

答案 3 :(得分:1)

它为我工作(选择)

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

答案 4 :(得分:0)

@monfresh @tomblomfield你可以做到

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

在医生模型中

...或者,我不确定你是否可以将它与formtastic一起使用,但你可以使用

这样的范围来选择范围
has_many :appointments :through => :patients do
  def with_patients
    includes(:patients)
  end
end

appointment.patient不再是n + 1

答案 5 :(得分:0)

如果您想在面板行中显示多个字段,可以使用以下视图:

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

要将它放在redit表单中,首先将appointment_ids放入允许列表:

permit_params: appointment_ids:[]

添加与表单有很多关系

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

如果没有编码错误,应该可以工作。

答案 6 :(得分:0)

关于#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

关于标题: - 它可以是假的或一些标签(字符串)

关于 allow_destroy: - 您可以设置它检查用户管理员权限,如here

重要 - 在医师模型中,确保具有

accepts_nested_attributes_for :appointments, allow_destroy: true

并且,在活动管理模型文件 - admin\physicians.rb - 中设置:

permit_params :name, appointments_attributes: [:patient_id, :_destroy, :id]
相关问题