Skip to content

Conversation

@compwron
Copy link
Collaborator

…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:

![alt text](https://media.giphy.com/media/1nP7ThJFes5pgXKUNf/giphy.gif)

…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>
@github-actions github-actions bot added ruby Pull requests that update Ruby code Tests! 🎉💖👏 labels Nov 28, 2025
@compwron compwron requested a review from Copilot November 28, 2025 23:35
@compwron compwron marked this pull request as ready for review November 28, 2025 23:37
@compwron compwron merged commit 7461dd2 into main Nov 28, 2025
15 checks passed
@compwron compwron deleted the fix-bugsnag branch November 28, 2025 23:37
Copy link
Contributor

Copilot AI left a 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.clear to case_contact_contact_types.destroy_all to 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.

Comment on lines +238 to +249
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
Copy link

Copilot AI Nov 28, 2025

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Pull requests that update Ruby code Tests! 🎉💖👏

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants