J’utilise la gemme ActiveAdmin dans mon projet.
J’ai 2 modèles utilisant has_many par association. Le schéma de firebase database est identique à l’exemple de RailsGuide. http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association has_many par le biais de l’association http://soffr.miximages.com/ruby-on-rails/has_many_through.png
Comment puis-je utiliser ActiveAdmin pour …
Merci a tous. 🙂
Pour 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
Pour 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
En réponse tomblomfield suivre la question dans les commentaires:
Essayez ce qui suit dans votre bloc AA ActiveAdmin.register Model:
controller do def scoped_collection YourModel.includes(:add_your_includes_here) end end
Cela devrait paresser charger toutes vos associations pour chaque page d’index dans une requête séparée
HTH
Il devrait résoudre le problème de requête 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
C’est du travail pour moi (avec choisi)
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
@monfresh @tomblomfield vous pouvez faire
has_many :appointments, ->{ includes(:patients) }, :through => :patients
dans le modèle des médecins
… ou je ne sais pas si vous pouvez l’utiliser avec formtastic mais vous pouvez rendre la scope facultative avec quelque chose comme
has_many :appointments :through => :patients do def with_patients includes(:patients) end end
et appointment.patient
vous.patient ne sera plus n + 1
Si vous souhaitez afficher plusieurs champs dans une rangée de panneaux, vous pouvez utiliser la vue suivante:
show do |phy| panel "Details" do atsortingbutes_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
Pour le placer dans votre formulaire redit, commencez par placer nomination_ids à la liste autorisée:
permit_params: appointment_ids:[]
Ajouter a de nombreuses relations avec la forme
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
Devrait fonctionner s’il n’y a pas d’erreur de codage.