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

发布于 2024-11-30 06:26:00 字数 708 浏览 1 评论 0原文

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

我有两个模型通过关联使用 has_many 。数据库架构看起来与 RailsGuide 中的示例完全相同。 http://guides.rubyonrails.org/association_basics.html#the-has_many -通过关联 has_many 通过关联
(来源:rubyonrails.org

如何使用 ActiveAdmin ...

  1. 在医生页面显示每位患者的预约日期?
  2. 在医生页面编辑每位患者的预约日期?

谢谢大家。 :)

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. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青萝楚歌 2024-12-07 06:26:00

对于 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

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
暗地喜欢 2024-12-07 06:26:00

在回答 tomblomfield 评论中的后续问题:

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

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

中延迟加载每个索引页的所有关联

这应该在单独的查询HTH

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

丘比特射中我 2024-12-07 06:26:00

应该可以解决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 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
公布 2024-12-07 06:26:00

这对我来说是工作(选择)

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

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
浅紫色的梦幻 2024-12-07 06:26:00

@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

@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

じее 2024-12-07 06:26:00

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

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

要将其放入您的重编辑表单中,首先将约会 ID 放入允许列表中:

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

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

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.

叶落知秋 2024-12-07 06:26:00

关于#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: - 你可以设置它检查用户管理员权限如此处

重要 所示- 在医师模型,请确保

accepts_nested_attributes_for :appointments, allow_destroy: true

在活动管理模型文件 - admin\Physicians.rb 中设置:

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

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 - admin\physicians.rb - set this:

permit_params :name, appointments_attributes: [:patient_id, :_destroy, :id]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文