has many through rails 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

Tags:

Misc Example