has many through ruby code example

Example 1: has many through rails

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

Example 2: activerecord through

class Doctor < ApplicationRecord
	has_many :patients, through: :consultation
end

Example 3: active record dependent destroy

has_many :books, dependent: :destroy

Tags:

Misc Example