Skip to content

Conversation

@rileyseaburg
Copy link
Contributor

Problem

The 'DOWNLOAD Collections as .csv' button at /admin/cx_collections/export_csv returns a 500 error ('We're sorry, but something went wrong').

Root Cause

The User#cx_collections method calls user_org.id without checking if user_org is nil. When a user has no organization, this throws:

NoMethodError: undefined method 'id' for nil:NilClass

Solution

  1. app/models/user.rb - Added early return CxCollection.none when user has no organization
  2. app/models/cx_collection.rb - Improved eager loading to include service_provider.organization and cx_collection_details to prevent N+1 queries
  3. Database migration - Added missing indexes on cx_collections, cx_collection_details, and cx_responses tables for better query performance

Testing

  • Verified the fix handles nil organization gracefully
  • CSV export now works for users with and without organizations

Related

Fixes issue reported by OMB (Bin Zheng) since Nov 19, 2025

- Handle nil organization in User#cx_collections to prevent NoMethodError
- Add eager loading for service_provider.organization and cx_collection_details
- Add missing database indexes on cx_collections, cx_collection_details, cx_responses

Fixes the 'We're sorry, but something went wrong' error at:
/admin/cx_collections/export_csv
Copilot AI review requested due to automatic review settings December 10, 2025 19:31
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 500 error in the CX collections CSV export functionality that occurred when users without an organization attempted to export data. The fix adds a nil check for user organization, improves query performance with better eager loading, and adds missing database indexes on foreign keys.

Key Changes:

  • Added nil organization check in User#cx_collections to return empty collection instead of raising NoMethodError
  • Improved eager loading in CxCollection.to_csv to include nested associations and prevent N+1 queries
  • Added database indexes on foreign key columns for cx_collections, cx_collection_details, and cx_responses tables

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
app/models/user.rb Added early return when user has no organization to prevent nil reference errors
app/models/cx_collection.rb Enhanced eager loading with nested associations and fixed redundant variable assignment on line 124
db/migrate/20251210192727_add_indexes_to_cx_collections.rb Added missing foreign key indexes to improve query performance

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rileyseaburg rileyseaburg merged commit 2a919af into develop Dec 10, 2025
5 of 6 checks passed
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