All models validation for SSN and TIN is working as intended except the Participant model. An error message won't display and the buttons grey out but it won't create as intended.

This commit is contained in:
Ben 2024-02-06 17:36:08 -06:00
parent b375d289fe
commit 35d144327b
10 changed files with 53 additions and 19 deletions

View File

@ -33,15 +33,17 @@ class ParticipantsController < ApplicationController
if @participant.save if @participant.save
redirect_to @participant, notice: 'Participant was successfully created.' redirect_to @participant, notice: 'Participant was successfully created.'
else else
render :new flash.now[:alert] = 'There was an error creating the participant.'
render :new, status: :unprocessable_entity
end end
end end
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
flash.now[:alert] = "Error: #{e.message}" flash.now[:alert] = "Error: #{e.message}"
render :new render :new, status: :unprocessable_entity
end end
def edit def edit
end end

View File

@ -16,6 +16,8 @@ class Employer < ApplicationRecord
validate :unique_as_participant validate :unique_as_participant
validates :ssn, uniqueness: true, allow_blank: true, presence: true
validates :tin, uniqueness: true, allow_blank: true, presence: true
private private

View File

@ -7,15 +7,21 @@ class Participant < ApplicationRecord
has_many :vendors, through: :service_contracts has_many :vendors, through: :service_contracts
has_many :employer_records has_many :employer_records
has_many :employers, through: :employer_records has_many :employers, through: :employer_records
has_and_belongs_to_many :programs
# Validations # Validations
validates :first_name, presence: true validates :first_name, :last_name, :ssn, presence: true
validates :last_name, presence: true validate :ssn_must_be_unique_across_models
def full_name def full_name
"#{first_name} #{last_name}" "#{first_name} #{last_name}"
end end
# Other associations private
has_and_belongs_to_many :programs
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 end

View File

@ -3,4 +3,7 @@ class Vendor < ApplicationRecord
has_and_belongs_to_many :employers has_and_belongs_to_many :employers
has_many :service_contracts has_many :service_contracts
has_many :participants, through: :service_contracts has_many :participants, through: :service_contracts
# Validations
validates :tin, uniqueness: true, allow_blank: true, presence: true
end end

View File

@ -12,6 +12,7 @@ class Worker < ApplicationRecord
# Validations # Validations
validates :first_name, presence: true validates :first_name, presence: true
validates :last_name, presence: true validates :last_name, presence: true
validates :ssn, uniqueness: true, allow_blank: true, presence: true
# Method to return the full name of the worker # Method to return the full name of the worker
def full_name def full_name

View File

@ -1,9 +1,9 @@
<%= form_with(model: participant, local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %> <%= form_with(model: participant, local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %>
<% if participant.errors.any? %> <% if @participant.errors.any? %>
<div id="error_explanation" class="alert alert-danger" role="alert"> <div id="error_explanation" class="alert alert-danger">
<h4><%= pluralize(participant.errors.count, "error") %> prohibited this participant from being saved:</h4> <h4><%= pluralize(@participant.errors.count, "error") %> prohibited this participant from being saved:</h4>
<ul> <ul>
<% participant.errors.full_messages.each do |message| %> <% @participant.errors.full_messages.each do |message| %>
<li><%= message %></li> <li><%= message %></li>
<% end %> <% end %>
</ul> </ul>

View File

@ -0,0 +1,6 @@
class AddIndexesToEmployers < ActiveRecord::Migration[7.0]
def change
add_index :employers, :ssn, unique: true
add_index :employers, :tin, unique: true
end
end

View File

@ -0,0 +1,5 @@
class AddIndexesToWorkers < ActiveRecord::Migration[7.1]
def change
add_index :workers, :ssn, unique: true
end
end

View File

@ -0,0 +1,5 @@
class AddIndexesToVendors < ActiveRecord::Migration[7.1]
def change
add_index :vendors, :tin, unique: true
end
end

6
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do ActiveRecord::Schema[7.1].define(version: 2024_02_06_215805) do
create_table "employer_records", force: :cascade do |t| create_table "employer_records", force: :cascade do |t|
t.integer "participant_id", null: false t.integer "participant_id", null: false
t.integer "employer_id", null: false t.integer "employer_id", null: false
@ -38,6 +38,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do
t.string "city" t.string "city"
t.string "state" t.string "state"
t.string "zip" t.string "zip"
t.index ["ssn"], name: "index_employers_on_ssn", unique: true
t.index ["tin"], name: "index_employers_on_tin", unique: true
end end
create_table "employers_vendors", id: false, force: :cascade do |t| create_table "employers_vendors", id: false, force: :cascade do |t|
@ -137,6 +139,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do
t.string "city" t.string "city"
t.string "state" t.string "state"
t.string "zip" t.string "zip"
t.index ["tin"], name: "index_vendors_on_tin", unique: true
end end
create_table "workers", force: :cascade do |t| create_table "workers", force: :cascade do |t|
@ -155,6 +158,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do
t.string "city" t.string "city"
t.string "state" t.string "state"
t.string "zip" t.string "zip"
t.index ["ssn"], name: "index_workers_on_ssn", unique: true
end end
add_foreign_key "employer_records", "employers" add_foreign_key "employer_records", "employers"