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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7099080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:47:55  来源:igfitidea点击:

How to use ActiveAdmin on models using has_many through association?

ruby-on-railsruby-on-rails-3rubygemsassociationsactiveadmin

提问by Victor Lam

I am using ActiveAdmingem in my project.

我在我的项目中使用ActiveAdmingem。

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-associationhas_many through association
(source: rubyonrails.org)

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

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?
  1. 在医生页面显示每位患者的预约日期?
  2. 在医生页面中编辑每位患者的预约日期?

Thanks all. :)

谢谢大家。:)

回答by PeterWong

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

回答by Nazar

In answer tomblomfield follow up question in comments:

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

Try the following in your AA ActiveAdmin.register Model do block:

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

  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

HTH

回答by Manikandan

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

回答by Evan Ross

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

回答by Tahsin Turkoz

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 表单中,首先将约会 ID 放入允许列表:

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.

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

回答by thrice801

@monfresh @tomblomfield you can do

@monfresh @tomblomfield 你可以做

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

...或者,我不确定您是否可以将它与 formtastic 一起使用,但您可以通过类似的方式使范围成为可选

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

and appointment.patientwont n+1 anymore

并且appointment.patient不再 n+1