rails has_many through 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: rails join table macro

create_join_table :posts, :tags

Example 4: active record dependent destroy

has_many :books, dependent: :destroy

Tags:

Ruby Example