2011-08-17 6 views
46

내 프로젝트에 ActiveAdmin 보석을 사용하고 있습니다.연관을 통해 has_many를 사용하는 모델에서 ActiveAdmin을 사용하는 방법은 무엇입니까?

has_many 연관을 사용하는 2 개의 모델이 있습니다. 데이터베이스 스키마는 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. 의사 페이지에서 각 환자의 예약 날짜를 편집 하시겠습니까?

감사합니다. 2의 경우 1 :

+0

입니까? –

+2

나는 그것을 만들지 않았다. 레일즈 가이드 웹 사이트에서 가져옵니다. –

+7

우리가 알아야 할 것은 그 다이어그램을 만드는 데 사용 된 것입니다! –

답변

70

)

show do 
    panel "Patients" do 
    table_for physician.appointments do 
     column "name" do |appointment| 
     appointment.patient.name 
     end 
     column :appointment_date 
    end 
    end 
end 

) 대답에

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

비슷한 일을하고 있습니다. SQL 악몽. appointment.patient를 호출 할 때마다 다른 SQL 쿼리가 생성됩니다. 표준 n + 1 쿼리 문제. 누구나 ActiveRecord의 열망하는로드를 수행하는 방법을 찾았습니까? 예 .includes (: patient) – tomblomfield

+0

해결책에 약간의 문제가 있습니다. _destroy 입력은 내 경우에 작동하지 않습니다 – Awea

+0

여러 가지 이유가있을 수 있으며 "작동하지 않습니다"는 정확히 무엇이 잘못되었는지 알기에 충분하지 않습니다. 나는 그것에 대해 죄송합니다. – PeterWong

13

는 의견에 질문을 후속 tomblomfield :

을 당신의 AA의 ActiveAdmin.register 모델에서 않는 다음을 시도 블록 :

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

이해야 별도의 쿼리의 각 인덱스 페이지에 대한 게으른로드하여 연결된 모든 사용자가 의사 모델

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

을 할 수 @tomblomfield @monfresh

HTH

+1

'scoped_collection'은'show' 뷰에서 호출되지 않기 때문에'show' 뷰에서 N + 1 개의 쿼리에 대해 이야기하고 있었기 때문에 tomblomfield의 문제는 해결되지 않습니다. 나도이 문제에 뛰어 들고있어 해결책을 찾지 못했다. – monfresh

+0

당신이 찾고있는 것이'def find_resource'라고 생각합니다. 여기서'Foo.includes (: bars) .find (params [: id])'를 실행할 수 있습니다. – Tashows

0

... 또는 formtastic과 함께 사용할 수 있는지 확신 할 수 없지만 범위를 선택적으로 만들 수 있는지 여부는 알 수 없습니다.

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

appointment.patient 더 이상 사용하지 않음

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 
0

당신이 패널 행에 표시 여러 필드를하려는 경우 다음보기 사용할 수 있습니다

permit_params: appointment_ids:[] 
: REDIT 허용 목록에 첫 번째 풋 appointment_ids을 형성 당신에 배치하기 위해

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 

추가는 양식과 많은 관계가 있습니다.

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 

코딩 오류가없는 경우 작동해야합니다.

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 
관련 문제