[英]How can I manipulate data on a has_many :through Association on ActiveRecords on Ruby on Rails?
The documentation is a little poor in this aspect. 文档在这方面有点差。 If I have this tables
如果我有这个表
Physicians Table
-----------
id integer
name string
Appointments table
----------------
id integer
physician_id integer
patient_id integer
appointment_date datetime
Patients table
---------------
id integer
name string
and this models: 这个型号:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :pshysician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :pshysicians, :through => :appointments
end
how do I do to insert an appointment on the database? 如何在数据库中插入约会? It's not clear at all in the Rails webpage
在Rails网页中根本不清楚
This is a quite broad question actually. 实际上,这是一个相当广泛的问题。 To insert a record in the database you can do one of three things.
要在数据库中插入记录,您可以执行以下三种操作之一。
Appointment.create(patient_id: 1, physician_id: 1, apt_date: DateTime.now)
If you have an instance of the Patient
model lets call it patient
and you want to assign in to physician with id=2
then 如果你有一个
Patient
模型的实例,我可以称之为patient
然后你想分配给id=2
医生
patient.appointments.create(physician_id: 2, apt_date: DateTime.now)
If you have an instance of the Physician
model called physician
then 如果您有一个名为
physician
的Physician
模型实例
physician.appointments.create(patient_id: 10, apt_date: DateTime.now)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.