[英]Creating has_many :through Association conditionally
我有以下課程:
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
我只希望醫師能夠創建一個約會,而不能創建一個病人。 請讓我知道如何在模型級別上具有此限制。
謝謝!
您無需使用關聯來檢索患者約會對象。 只需為他們創建一個getter方法:
class Patient < ActiveRecord::Base
def appointments
Appointment.where(patient_id: self.id)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.