2024-01-16 20:54:44 -06:00
|
|
|
class Employer < ApplicationRecord
|
2024-02-05 15:58:46 -06:00
|
|
|
# Direct association with participants
|
|
|
|
has_many :direct_participants, class_name: 'Participant'
|
2024-02-02 23:12:36 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association through EmployerRecord
|
|
|
|
has_many :employer_records
|
|
|
|
has_many :indirect_participants, through: :employer_records, source: :participant
|
2024-01-29 22:04:56 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association with Workers through direct_participants
|
|
|
|
has_many :workers, through: :direct_participants
|
|
|
|
|
|
|
|
# Other methods...
|
2024-02-05 16:18:06 -06:00
|
|
|
def full_name
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
end
|
2024-02-06 00:24:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
validate :unique_as_participant
|
2024-02-06 17:36:08 -06:00
|
|
|
validates :ssn, uniqueness: true, allow_blank: true, presence: true
|
|
|
|
validates :tin, uniqueness: true, allow_blank: true, presence: true
|
2024-02-06 00:24:38 -06:00
|
|
|
|
|
|
|
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
|