diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index acc3865..1d113fa 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -33,15 +33,17 @@ class ParticipantsController < ApplicationController if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else - render :new + flash.now[:alert] = 'There was an error creating the participant.' + render :new, status: :unprocessable_entity end end rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = "Error: #{e.message}" - render :new + render :new, status: :unprocessable_entity end + def edit end diff --git a/app/models/employer.rb b/app/models/employer.rb index 0799c41..b7c96b1 100644 --- a/app/models/employer.rb +++ b/app/models/employer.rb @@ -16,6 +16,8 @@ class Employer < ApplicationRecord validate :unique_as_participant + validates :ssn, uniqueness: true, allow_blank: true, presence: true + validates :tin, uniqueness: true, allow_blank: true, presence: true private diff --git a/app/models/participant.rb b/app/models/participant.rb index 0cf49b2..b6f6a0e 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -7,15 +7,21 @@ class Participant < ApplicationRecord 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, presence: true - validates :last_name, presence: true + validates :first_name, :last_name, :ssn, presence: true + validate :ssn_must_be_unique_across_models + def full_name "#{first_name} #{last_name}" end - # Other associations - has_and_belongs_to_many :programs -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 \ No newline at end of file diff --git a/app/models/vendor.rb b/app/models/vendor.rb index 381a29c..398ce6a 100644 --- a/app/models/vendor.rb +++ b/app/models/vendor.rb @@ -3,4 +3,7 @@ class Vendor < ApplicationRecord has_and_belongs_to_many :employers has_many :service_contracts has_many :participants, through: :service_contracts + + # Validations + validates :tin, uniqueness: true, allow_blank: true, presence: true end diff --git a/app/models/worker.rb b/app/models/worker.rb index f124b89..d861cbb 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -12,6 +12,7 @@ class Worker < ApplicationRecord # Validations validates :first_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 def full_name diff --git a/app/views/participants/_form.html.erb b/app/views/participants/_form.html.erb index 9e84ea6..833035c 100644 --- a/app/views/participants/_form.html.erb +++ b/app/views/participants/_form.html.erb @@ -1,14 +1,14 @@ <%= form_with(model: participant, local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %> - <% if participant.errors.any? %> - - <% end %> + <% if @participant.errors.any? %> +
+

<%= pluralize(@participant.errors.count, "error") %> prohibited this participant from being saved:

+ +
+<% end %>
<%= form.label :first_name, 'First Name', class: 'form-label' %> diff --git a/db/migrate/20240206215026_add_indexes_to_employers.rb b/db/migrate/20240206215026_add_indexes_to_employers.rb new file mode 100644 index 0000000..ef7585b --- /dev/null +++ b/db/migrate/20240206215026_add_indexes_to_employers.rb @@ -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 diff --git a/db/migrate/20240206215704_add_indexes_to_workers.rb b/db/migrate/20240206215704_add_indexes_to_workers.rb new file mode 100644 index 0000000..750254c --- /dev/null +++ b/db/migrate/20240206215704_add_indexes_to_workers.rb @@ -0,0 +1,5 @@ +class AddIndexesToWorkers < ActiveRecord::Migration[7.1] + def change + add_index :workers, :ssn, unique: true + end +end diff --git a/db/migrate/20240206215805_add_indexes_to_vendors.rb b/db/migrate/20240206215805_add_indexes_to_vendors.rb new file mode 100644 index 0000000..5bc20a6 --- /dev/null +++ b/db/migrate/20240206215805_add_indexes_to_vendors.rb @@ -0,0 +1,5 @@ +class AddIndexesToVendors < ActiveRecord::Migration[7.1] + def change + add_index :vendors, :tin, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4b695ab..23e79b7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.integer "participant_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 "state" t.string "zip" + t.index ["ssn"], name: "index_employers_on_ssn", unique: true + t.index ["tin"], name: "index_employers_on_tin", unique: true end 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 "state" t.string "zip" + t.index ["tin"], name: "index_vendors_on_tin", unique: true end 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 "state" t.string "zip" + t.index ["ssn"], name: "index_workers_on_ssn", unique: true end add_foreign_key "employer_records", "employers"