Skip to content

Add uniqueness validation for contact email scoped to sponsor#2713

Merged
mroderick merged 1 commit into
masterfrom
add-contact-uniqueness-validation
Jul 16, 2026
Merged

Add uniqueness validation for contact email scoped to sponsor#2713
mroderick merged 1 commit into
masterfrom
add-contact-uniqueness-validation

Conversation

@mroderick

@mroderick mroderick commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Problem

ActiveRecord::RecordNotUnique (PG::UniqueViolation) raised when a duplicate contact email is submitted for the same sponsor. The DB has a unique index on (email, sponsor_id), but there is no model-level validation, so valid? passes and save blows up with a 500.

Root cause: double-submit on the sponsor edit form — two POSTs arrive, both carrying the same new contact (no id). First INSERT succeeds, second hits the unique constraint.

https://app.rollbar.com/a/codebar-production/fix/item/codebar-production/705#detail

Fix

Added validates :email, uniqueness: { scope: :sponsor_id } to Contact. Now valid? catches the duplicate before reaching the DB, and the controller re-renders the form with a validation error instead of a 500.

Changes

  • app/models/contact.rb — add uniqueness validation
  • spec/models/contact_spec.rb — new model spec for Contact validations

@mroderick
mroderick force-pushed the add-contact-uniqueness-validation branch from 3e626e8 to 614d21d Compare July 16, 2026 09:06
@mroderick
mroderick marked this pull request as ready for review July 16, 2026 09:11
@mroderick
mroderick merged commit 7acd393 into master Jul 16, 2026
9 checks passed
@mroderick
mroderick deleted the add-contact-uniqueness-validation branch July 16, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants