Skip to content

Fix events coach/student 500 when member already has an event invitation - #2795

Merged
mroderick merged 3 commits into
masterfrom
fix/event-invitation-nil-token
Aug 9, 2026
Merged

Fix events coach/student 500 when member already has an event invitation#2795
mroderick merged 3 commits into
masterfrom
fix/event-invitation-nil-token

Conversation

@mroderick

@mroderick mroderick commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Closes #2794

Summary

Fixes a 500 when a logged-in member clicks Coach/Student on an event where they already have an Invitation row for that event + role with attending NULL.

Related Rollbar items

  • Rollbar 712EventsController#student, event 448 (virtual-open-day-8)
  • Rollbar 713EventsController#coach, event 448 (virtual-open-day-8)

Both are the same defect triggered through the student and coach actions respectively.

Root cause

find_invitation_and_redirect_to_event used Invitation.create_or_find_by. In Rails 8.1, create_or_find_by calls the non-bang create. When the uniqueness validation fails (row already exists), it returns an unpersisted record with token: nil. Because Invitation#to_param returns token, the subsequent redirect_to event_invitation_path raises ActionController::UrlGenerationError. Same bug as #2790, on the events/Invitation path — #2791 only covered workshops.

Fix

After create_or_find_by, check persisted?. If not persisted, fall back to Invitation.find_by(event:, member:, role:) to return the existing row.

Verification

  • Added controller specs for GET #student and GET #coach covering:
    • Existing nil-attending invitation for the event + role redirects to the existing invitation page.
    • No new invitation is created in that case.
    • Member without an invitation still creates one and redirects.
  • Reproduced the exact production scenario against the codebar_production_dump for both Rollbar items:
    • 712: members 3863/29087 for event 448 (virtual-open-day-8), Student role
    • 713: members 19217/29875 for event 448 (virtual-open-day-8), Coach role
    • All four now route to their existing invitation with a valid /events/virtual-open-day-8/invitation/:token URL.
  • Ran controller + request specs: 145 examples, 0 failures.
  • bundle exec rubocop app/controllers/events_controller.rb spec/controllers/events_controller_spec.rb — no offenses.

Post-Deploy Monitoring & Validation

  • Monitor error tracking for ActionController::UrlGenerationError with controller: "invitations" and token: nil in EventsController#find_invitation_and_redirect_to_event.
  • Expected signal: zero occurrences across both Rollbar items (712 and 713) after deploy.

`Invitation.create_or_find_by` in `find_invitation_and_redirect_to_event`
returns an unsaved record (id/token nil) when the member already has an
invitation for the event+role with attending nil, because the uniqueness
validation fails but create_or_find_by only rescues RecordNotUnique. The
subsequent `redirect_to event_invitation_path` then raises a
UrlGenerationError for a missing token.

Fall back to the existing persisted invitation when create_or_find_by
returns an unpersisted record. Same fix as #2791 applied to the events path.

Fixes #2794
@mroderick
mroderick requested a review from olleolleolle August 8, 2026 15:44
@mroderick
mroderick marked this pull request as ready for review August 8, 2026 15:44
@mroderick
mroderick force-pushed the fix/event-invitation-nil-token branch from 25a63fa to 0c8eef2 Compare August 8, 2026 17:07
@mroderick
mroderick enabled auto-merge August 9, 2026 12:01
@mroderick
mroderick merged commit 8305a7c into master Aug 9, 2026
9 checks passed
@mroderick
mroderick deleted the fix/event-invitation-nil-token branch August 9, 2026 12:08
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.

EventsController#coach/#student crash with 'No route matches ... token: nil' when member already has an event invitation

2 participants