class Employer < ApplicationRecord # Direct association with participants has_many :direct_participants, class_name: 'Participant' # Association through EmployerRecord has_many :employer_records has_many :indirect_participants, through: :employer_records, source: :participant # Association with Workers through direct_participants has_many :workers, through: :direct_participants # Other methods... def full_name "#{first_name} #{last_name}" end validate :unique_as_participant validates :ssn, uniqueness: true, allow_blank: true, presence: true validates :tin, uniqueness: true, allow_blank: true, presence: true private def unique_as_participant # Check if there's a Participant with the same details existing_participant = Participant.find_by(ssn: ssn) if existing_participant && (existing_participant.id != id) errors.add(:base, 'A participant with the same SSN already exists') end end end