Kenny D.
Kenny D.

Reputation: 937

One-to-many relationship when one side is unnecessary

I have a table called vital_sign which belongs to a patient (the patient has multiple vital signs) and to a physician (the physician captured this vital sign), but I don't care about getting physician.vital_signs, how do I express it in rails models?

I suspect something like this:

  1. vital_signs (belongs_to :patient, belongs_to :physician) with patient_id, physician_id
  2. patient (has_many :vital_signs)
  3. physician (nil)

Is this correct?

Upvotes: 0

Views: 84

Answers (1)

Dinshaw Raje
Dinshaw Raje

Reputation: 963

You can try this :

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :vital_signs
end

class VitalSign < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :vital_signs
end

Upvotes: 0

Related Questions