私はちょうど協会に飛び込んだhas_many :through
。Physician
3つのテーブル( 、Patient
および関連付けテーブル)すべてのデータを1つのフォームで保存する機能を実装しようとしています。
私の移行:
class CreatePhysicians < ActiveRecord::Migration
def self.up
create_table :physicians do |t|
t.string :name
t.timestamps
end
end
end
class CreatePatients < ActiveRecord::Migration
def self.up
create_table :patients do |t|
t.string :name
t.timestamps
end
end
end
class CreateAppointments < ActiveRecord::Migration
def self.up
create_table :appointments do |t|
t.integer :physician_id
t.integer :patient_id
t.date :appointment_date
t.timestamps
end
end
end
私のモデル:
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
accepts_nested_attributes_for :appointments
accepts_nested_attributes_for :physicians
end
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
accepts_nested_attributes_for :patients
accepts_nested_attributes_for :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
私のコントローラー:
def new
@patient = Patient.new
@patient.physicians.build
@patient.appointments.build
end
私の見解(new.html.rb
):
<% form_for(@patient) do |patient_form| %>
<%= patient_form.error_messages %>
<p>
<%= patient_form.label :name, "Patient Name" %>
<%= patient_form.text_field :name %>
</p>
<% patient_form.fields_for :physicians do |physician_form| %>
<p>
<%= physician_form.label :name, "Physician Name" %>
<%= physician_form.text_field :name %>
</p>
<% end %>
<p>
<%= patient_form.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', patients_path %>
の新しい関連レコードを作成することはできますがPatient
、今度はフォームのフィールドも作成したいと思います。sのフィールドはどこに配置する必要があり、コントローラーにはどのような変更が必要ですか?私はグーグルを試し、これを試しましたが、それを実装する際に何らかのエラーで立ち往生しました。Physician
Appointment
appointment_date
Appointment