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 class Employer < ApplicationRecord # Associations and other methods as before... validate :unique_as_participant, if: -> { ssn_changed? || new_record? } validates :ssn, uniqueness: { allow_blank: true }, if: -> { ssn.present? } validates :tin, uniqueness: { allow_blank: true }, if: -> { tin.present? } private def unique_as_participant if Participant.exists?(ssn: ssn) && (id.blank? || Participant.where.not(id: id).exists?(ssn: ssn)) errors.add(:ssn, 'A participant with the same SSN already exists') end end end end