[英]Ruby on Rails - Accessing data in :has_many => :through association
[英]How can I manipulate data on a has_many :through Association on ActiveRecords on Ruby on Rails?
文檔在這方面有點差。 如果我有這個表
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
這個型號:
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
如何在數據庫中插入約會? 在Rails網頁中根本不清楚
實際上,這是一個相當廣泛的問題。 要在數據庫中插入記錄,您可以執行以下三種操作之一。
Appointment.create(patient_id: 1, physician_id: 1, apt_date: DateTime.now)
如果你有一個Patient
模型的實例,我可以稱之為patient
然后你想分配給id=2
醫生
patient.appointments.create(physician_id: 2, apt_date: DateTime.now)
如果您有一個名為physician
的Physician
模型實例
physician.appointments.create(patient_id: 10, apt_date: DateTime.now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.