-
-
Notifications
You must be signed in to change notification settings - Fork 511
fix: Prevent uniqueness validation error when updating case contact t… #6614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ypes Fixes Bugsnag error "Validation failed: Case contact has already been taken" (Error URL: https://api.bugsnag.com/projects/5f370b68136a4b0010d15364/events/6924fd6f015b929d46570000) The issue occurred when updating case contacts with contact types. The remove_unwanted_contact_types method used contact_types.clear which marks records for deletion in memory but doesn't immediately execute DELETE statements. When the subsequent update attempted to create new join records, the old records still existed in the database, triggering the uniqueness validation on CaseContactContactType. Changed to use destroy_all which immediately deletes records from the database, ensuring old contact type associations are fully removed before new ones are created. Added regression test to prevent this issue from recurring. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a uniqueness validation error that occurred when updating case contacts with contact types. The issue was caused by using contact_types.clear, which marks records for deletion in memory but doesn't immediately execute DELETE statements. When the update attempted to create new join records, the old records still existed in the database, triggering the uniqueness validation on CaseContactContactType.
Key changes:
- Changed from
contact_types.cleartocase_contact_contact_types.destroy_allto ensure old associations are immediately deleted from the database - Added regression test to verify the fix works when re-assigning the same contact type
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| app/controllers/case_contacts/form_controller.rb | Changed remove_unwanted_contact_types to use destroy_all instead of clear for immediate database deletion |
| spec/requests/case_contacts/form_spec.rb | Added regression test for re-assigning the same contact type without uniqueness errors |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it "allows re-assigning the same contact type without uniqueness validation error" do | ||
| # This test prevents regression of Bugsnag error: | ||
| # "Validation failed: Case contact has already been taken" | ||
| case_contact.update!(contact_type_ids: [contact_types.first.id]) | ||
| expect(case_contact.contact_type_ids).to contain_exactly(contact_types.first.id) | ||
|
|
||
| # Re-submit form with same contact type (simulates user editing and saving) | ||
| request | ||
|
|
||
| case_contact.reload | ||
| expect(case_contact.contact_type_ids).to contain_exactly(contact_types.first.id) | ||
| end |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test should verify that the request succeeds (e.g., returns a redirect status) to explicitly confirm that no validation error occurred. Consider adding expect(response).to have_http_status(:redirect) after line 245 to match the pattern used in the 'dedupes and updates the contact type ids' test at line 221.
…ypes
Fixes Bugsnag error "Validation failed: Case contact has already been taken"
(Error URL: https://api.bugsnag.com/projects/5f370b68136a4b0010d15364/events/6924fd6f015b929d46570000)
The issue occurred when updating case contacts with contact types. The remove_unwanted_contact_types method used contact_types.clear which marks records for deletion in memory but doesn't immediately execute DELETE statements. When the subsequent update attempted to create new join records, the old records still existed in the database, triggering the uniqueness validation on CaseContactContactType.
Changed to use destroy_all which immediately deletes records from the database, ensuring old contact type associations are fully removed before new ones are created.
Added regression test to prevent this issue from recurring.
🤖 Generated with Claude Code
What github issue is this PR for, if any?
Resolves #XXXX
What changed, and why?
How is this tested? (please write rspec and jest tests!) 💖💪
Note: if you see a flake in your test build in github actions, please post in slack #casa "Flaky test: " :) 💪
Note: We love capybara tests! If you are writing both haml/js and ruby, please try to test your work with tests at every level including system tests like https://github.com/rubyforgood/casa/tree/main/spec/system
Screenshots please :)
Run your local server and take a screenshot of your work! Try to include the URL of the page as well as the contents of the page.
Feelings gif (optional)
What gif best describes your feeling working on this issue? https://giphy.com/
How to embed:
