I am using ActiveAdmin gem in my project.
我正在我的项目中使用ActiveAdmin gem。
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 http://guides.rubyonrails.org/images/has_many_through.png
我有两个通过关联使用has_many的模型。数据库模式与RailsGuide中的示例完全相同。http://guides.rubyonrails.org/association_basics.html#the has_many-through-association has_many通过association http://guides.rubyonrails.org/images/has_many_through.png
How can I use ActiveAdmin to ...
如何使用ActiveAdmin来…
- show appointment date of each patient in physicians page?
- 在医生页面显示每个病人的预约日期?
- edit appointment date of each patient in physicians page?
- 编辑医生页面上每个病人的预约日期?
Thanks all. :)
谢谢所有。:)
6 个解决方案
#1
73
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
#2
13
In answer tomblomfield follow up question in comments:
在回答tomblomfield的问题后,请回答以下问题:
Try the following in your AA ActiveAdmin.register Model do block:
在您的AA ActiveAdmin中尝试以下操作。注册模型做块:
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
#3
1
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
#4
1
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
#5
0
@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.patient
wont n+1 anymore
和约会。病人不会n + 1了
#6
0
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:
要将它放在您的重拨表中,请先将appointment ment_id放入允许列表:
permit_params: appointment_ids:[]
Add has many relationship to the form
Add与表单有很多关系
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.
如果没有编码错误,应该可以工作。
#1
73
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
#2
13
In answer tomblomfield follow up question in comments:
在回答tomblomfield的问题后,请回答以下问题:
Try the following in your AA ActiveAdmin.register Model do block:
在您的AA ActiveAdmin中尝试以下操作。注册模型做块:
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
#3
1
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
#4
1
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
#5
0
@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.patient
wont n+1 anymore
和约会。病人不会n + 1了
#6
0
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:
要将它放在您的重拨表中,请先将appointment ment_id放入允许列表:
permit_params: appointment_ids:[]
Add has many relationship to the form
Add与表单有很多关系
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.
如果没有编码错误,应该可以工作。