class Participant < ApplicationRecord # Associations belongs_to :employer, optional: true has_many :employments has_many :workers, through: :employments has_many :service_contracts has_many :vendors, through: :service_contracts has_many :employer_records has_many :employers, through: :employer_records has_and_belongs_to_many :programs # Validations validates :first_name, :last_name, :ssn, presence: true validate :ssn_must_be_unique_across_models def full_name "#{first_name} #{last_name}" end private def ssn_must_be_unique_across_models if Participant.where.not(id: id).exists?(ssn: ssn) || Employer.exists?(ssn: ssn) errors.add(:ssn, 'SSN is already taken by another participant or employer') end end end