rails初心者です。
DB設計において、下記のような中間テーブルを用いる状況に遭遇しました。
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
m:nのリレーションを記述する際、中間テーブルを用いて、
has_many :through の関係を記述する必要があることは、理解できました。
しかし、Physicianテーブルに関して、
何故 has_many :appointments の記述の必要性があるのか納得できていません。
(Patientテーブルの has_many :appointmentsも同様です。)
has_many :patients, through: :appointments
と記述した時点で中間テーブルを認識して、集合論的に大きいhas_many :appointments の記述はしなくてもいいのではないかと感じました。
これはrailsの定義的な問題のため、そのまま理解するしかないのでしょうか。
少しでも補足情報があればご教授いただけますと幸いです。
参考サイト
参考箇所
2.4 has_many :through関連付け
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。