Rails has_many: через вложенную форму
Я только что прыгнул вhas_many :through
ассоциация. Я'm пытается реализовать возможность сохранения данных для всех 3 таблиц (,Physician
Patient
и таблица ассоциаций) через единую форму.
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
):Я'
<p>
</p>
<p>
</p>
<p>
</p>
я могу создать новый,Patient
Physician
и связанная запись дляAppointment
, но теперь я хочу иметь поле дляappointment_date
тоже по форме. Где я должен разместить поля дляAppointment
s и какие изменения требуются в моем контроллере? Я пытался гуглить и пыталсяэтот, но застрял в той или иной ошибке его реализации.