Skip to content

fix: make sponsor coach spots nil-safe - #2787

Merged
mroderick merged 2 commits into
masterfrom
fix/sponsor-coach-spots
Aug 9, 2026
Merged

fix: make sponsor coach spots nil-safe#2787
mroderick merged 2 commits into
masterfrom
fix/sponsor-coach-spots

Conversation

@mroderick

@mroderick mroderick commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Problem

Linked to #2500. The admin sponsor show page returned an HTTP 500 for some sponsor records.

Sponsor#coach_spots did number_of_coaches || (seats / 2.0).round, which raises NoMethodError: undefined method '/' for nil when both number_of_coaches and seats are nil. The show view calls this unconditionally, so any incomplete record crashes.

The #1794 fix (adding presence validations + HTML5 required) prevents new bad rows, but pre-existing records can still hold nil values, so coach_spots remains crash-prone.

Change

Make coach_spots nil-safe: return number_of_coaches when present, else half the seats rounded, else nil instead of raising.

Tests

Added three #coach_spots cases to spec/models/sponsor_spec.rb. The nil-both case fails on the old implementation and passes with this fix.

Database analysis

Checked the codebar_dump Postgres database for records that would trigger this crash. Querying for sponsors rows where seats IS NULL OR number_of_coaches IS NULL returned 0 rows across all 751 sponsors. The broken records referenced in #2500 (IDs 1539 and 1540) are absent from the dump — only the valid 1541 (BDO UK, seats 0 / coaches 0) survives — so the bad rows appear to have already been removed in production. No data cleanup is required. The 0 values on remaining rows are valid (validation allows >= 0), so this fix is defensive hardening against recurrence through any non-controller data path.

Note: the "duplicate sponsor on update" aspect of #2500 was not reproducible — no code path in the update/create actions creates a new Sponsor row.

Sponsor#coach_spots raised `undefined method '/' for nil` when both
number_of_coaches and seats were nil, crashing the admin show page with
a 500. The #1794 validations prevent new bad rows but not pre-existing
ones, so guard the fallback instead of assuming seats is present.

Fixes #2500
@mroderick
mroderick marked this pull request as ready for review August 6, 2026 08:01
@mroderick
mroderick enabled auto-merge August 9, 2026 11:08
@mroderick
mroderick merged commit 89c88af into master Aug 9, 2026
9 checks passed
@mroderick
mroderick deleted the fix/sponsor-coach-spots branch August 9, 2026 11:11
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.

2 participants