新年快樂給大家!導軌5:「belongs_to的通過」類型的關聯
我有經典has_many through association:
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
但是現在我需要從class Appointment
添加has_many
聯想到一些class Example
,這與其他模型belongs_to
協會將在belongs_to
伴隨class Example
。
如果可能的話,如何設置這種assosiation的?謝謝。
更新
我不明白,爲什麼這個問題是downvoted。 以下是我需要class Example
:
class Example < ApplicationRecord
belongs_to :appointment
belongs_to :model_bbb
end
更新2
好吧,我想通了,我可以使用解決方案從this answer。基本上我可以刪除「預約」的模式,並有class Example
這樣的:
class Example < ApplicationRecord
belongs_to :physician
belongs_to :patient
belongs_to :model_bbb
end
然後在醫生和病人,我可以做has_many :examples
和另一through
關係。我也想做一些奇怪的belongs_to through
東西我能有class Appointment
相對小桌子,但class Example
表預計將相當大。所以我最初的想法是不創建額外的列,這將被重複多次。
問題是不理解。那麼,你最後的片段有什麼問題?順便說一句,請編輯標籤以改善分類 - 因爲這是一個ruby-on-rails-5線程。 – marmeladze