同様の問題に対するすべての解決策を試しましたが、これはわかりませんでした。
「Clinician」と「Patient」の間に has_many :through 関係があり、モデル「CareGroupAssignment」が結合されています。これまでに試した方法では、clinician
topatient
関連付けを保存できませんでした。複数を関連付けることができるようにしたいのですが、複数を持つことpatient
になります。clinicians
clinicians
patients
clinician.rb (簡略化)
class Clinician < ActiveRecord::Base
belongs_to :care_group
has_many :patients ,:through=> :care_group_assignments
has_many :care_group_assignments, :dependent => :destroy
belongs_to :user
accepts_nested_attributes_for :user, :allow_destroy => true
end
患者.rb
class Patient < ActiveRecord::Base
belongs_to :care_group
has_many :clinicians ,:through=> :care_group_assignments
has_many :care_group_assignments
belongs_to :user
accepts_nested_attributes_for :user, :allow_destroy => true
end
care_group_assignments.rb
class CareGroupAssignment < ActiveRecord::Base
belongs_to :clinician
belongs_to :patient
end
まず、 Railscasts PRO #17 - HABTM チェックボックスの例に従って、少なくともデータの収集を開始し、モデルを正しく設定しようとしました。以下は、RailsCast で説明されている各臨床医のチェックボックスを含むフォームです。チェックボックスが表示され、データは送信されますが、保存されません (理由がわからない)。
患者new.html.erbフォーム
<%= form_for @patient do |form| %>
<%= form.fields_for :user do |builder| %>
<div class="form-group">
<%= builder.label "Email or Username" %>
<%= builder.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= builder.label :password %>
<%= builder.password_field :password, class: "form-control" %>
</div>
<% end %>
<div class="form-group">
<%= form.label :first_name %>
<%= form.text_field :first_name, class: "form-control", placeholder: "First name" %>
</div>
<div class="form-group">
<%= form.label :last_name %>
<%= form.text_field :last_name, class: "form-control", placeholder: "Last name" %>
</div>
<div class="form-group">
<% Clinician.where(care_group_id: @care_group.id).each do |clinician| %>
<%= check_box_tag "patient[clinician_ids][]", clinician.id, @patient.clinician_ids.include?(clinician.id), id: dom_id(clinician) %>
<%= label_tag dom_id(clinician), clinician.full_name %><br>
<% end %>
</div>
<%= form.button 'Create Patient', class: "btn btn-u btn-success" %>
<% end %>
次に、この質問collection_select
に対する答えを試してみました。これにより、1 つしか選択できない不適切な形式のリストが作成されます。データは送信されたようですが、保存されません。clinician
患者new.html.erbフォーム
<div class="form-group">
<%= collection_select(:patient, :clinician_ids,
Clinician.where(care_group_id: @care_group.id).order("first_name asc"),
:id, :full_name, {:selected => @patient.clinician_ids, :include_blank => true}, {:multiple => true}) %>
</div>
最後に、この質問/解決策で行われたことをコピーしました。また、通常のドロップダウンとしてフォーマットされていませんが、代わりに、1 つだけを選択できるcollection_select
境界線で囲まれたリストです。clinician
患者new.html.erbフォーム
<div class="form-group">
<% Clinician.where(care_group_id: @care_group.id).each do |clinician| %>
<%= check_box_tag "patient[clinician_ids][]", clinician.id, @patient.clinician_ids.include?(clinician.id), id: dom_id(clinician) %>
<%= label_tag dom_id(clinician), clinician.full_name %><br>
<% end %>
</div>
これまでのところ、これらのメソッドのいずれもclinician
topatient
関連付けを保存できませんでした。
Patient_controller.rb
def new
@patient = Patient.new
@user = User.new
@patient.build_user
@care_group = current_clinician.care_group
end
def create
@patient = Patient.create(patient_params)
@patient.care_group = current_clinician.care_group
if @patient.save
redirect_to patient_path(@patient), notice: "New patient created!"
else
render "new"
end
end
def show
@patient = Patient.find_by(id: params["id"])
end
private
def patient_params
params.require(:patient).permit({:clinician_ids => [:id]},:first_name,:last_name,:user_id,:care_group_id, user_attributes: [ :email, :password, :patient_id, :clinician_id ])
end
clinicians
に関連付けられているpatient
を患者show
ページに表示する予定です。
患者show.html.erb
<strong>Shared with:</strong>
<% @patient.clinicians.each do |clinician| %>
<%= clinician.full_name %><
<% end %>
データベースをシードするとこれは機能しますが、データが保存されていないように見えるため、何も表示されません。
Rails 4.1.8、ルビー 2.2.1p85、PostgreSQL
ありがとう