Skip to content

Add model-level uniqueness validation on Subscription#68

Open
bellisabell wants to merge 1 commit intomainfrom
bell/add-subscription-uniqueness
Open

Add model-level uniqueness validation on Subscription#68
bellisabell wants to merge 1 commit intomainfrom
bell/add-subscription-uniqueness

Conversation

@bellisabell
Copy link
Copy Markdown
Contributor

Summary

Fixes #30 - Missing model-level uniqueness validation on Subscription

Changes

  1. Model validation: Added validates :email_address_id, uniqueness: { scope: :account_id } to the Subscription model
  2. Race condition handling: Added find_or_create_by_with_rescue class method that rescues ActiveRecord::RecordNotUnique and falls back to finding the existing record
  3. Updated all creation sites to use the new helper:
    • PublicPagesController#create
    • SubscribersImportJob#import
    • Account#subscribe_to_updates

Why both?

  • The model validation catches duplicates during normal operation and provides a friendly error message
  • The RecordNotUnique rescue handles the race condition where two concurrent requests both pass the validation but one fails at the DB constraint level

Testing

  • The existing DB index ensures data integrity
  • The new validation will catch duplicates before hitting the database
  • The rescue handler prevents 500 errors from race conditions

- Add validates :email_address_id, uniqueness: { scope: :account_id }
- Add find_or_create_by_with_rescue helper to handle RecordNotUnique
- Update all subscription creation sites to use the rescue helper

Closes #30
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.

Bug: Missing model-level uniqueness validation on Subscription

2 participants