【发布时间】:2023-03-15 22:30:01
【问题描述】:
我在 db 中有如下迁移脚本的表:
class CreateAppointments < ActiveRecord::Migration
def change
create_table :physicians do |t|
t.string :name
t.timestamps null: false
end
create_table :patients do |t|
t.string :name
t.timestamps null: false
end
create_table :appointments do |t|
t.belongs_to :physician, index: true
t.belongs_to :patient, index: true
t.datetime :appointment_date
t.timestamps null: false
end
end
end
模型类看起来像:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
我可以使用以下方法选择特定医生的所有患者数据:
patients = Physician.find(123).patients
问题是,我需要存储在约会表中的约会日期以及患者详细信息。不知何故,我无法弄清楚如何获取它。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord