class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
关系如上,医生,病人,预约,很好理解,不过如果想设计 Physician 和 Patient 同属于一个表 User,应该怎么写呢?难道要用 Polymorphic Associations?感觉这样设计有点别扭。请指教。谢谢!
class User < ApplicationRecord
belongs_to :role, polymorphic: true
end
class Physician < ApplicationRecord
has_many :users, as: :role
end
class Patient < ApplicationRecord
has_many :users, as: :role
end