Commit 4bd3c78
committed
fix: handle duplicate InvitationLogEntry on retry
Second attempt at fixing the "Member has already been taken" validation error
when re-sending workshop invitations.
Problem:
The previous fix (PR #2556) used find_or_initialize_by with the status included
in the query. However, the InvitationLogEntry uniqueness constraint only validates
(member_id, invitation_type, invitation_id), NOT status. This means:
- A member can have ONE success entry AND ONE failure entry for the same invitation
- find_or_initialize_by(member, invitation, :success) would return a persisted
entry with status=:failed if one existed, causing the wrong status to be used
- Or if building a new entry with status=:success when status=:failed already
existed, the uniqueness validation would fail
Fix:
1. Split the check: first find any existing entry by (member, invitation),
ignoring status
2. If found, return it (either success or failure)
3. Only if no entry exists, create a new one with the specified status
Additionally, add autosave: false to the has_many :entries association to prevent
Rails from attempting to autosave unpersisted entries when fail_batch is called,
which was causing "Validation failed: Entries is invalid" errors.
Changes:
- app/services/invitation_logger.rb: Separate find_or_build_entry logic
- app/models/invitation_log.rb: Add autosave: false to entries association
Tests:
- All 41 existing tests pass
- Retry behavior tests verify no duplicate entries are created1 parent cbf3d60 commit 4bd3c78
2 files changed
+8
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
| |||
0 commit comments