Skip to content

Improve performance of workshop idea forms#1008

Open
Copilot wants to merge 5 commits intomainfrom
copilot/speed-up-forms-performance
Open

Improve performance of workshop idea forms#1008
Copilot wants to merge 5 commits intomainfrom
copilot/speed-up-forms-performance

Conversation

Copy link
Contributor

Copilot AI commented Feb 15, 2026

What is the goal of this PR and why is this important?

This PR optimizes database queries in the set_form_variables methods of three controllers (WorkshopIdeasController, StoryIdeasController, and WorkshopVariationIdeasController) to improve form loading performance. These forms were experiencing slow load times due to inefficient queries that loaded unnecessary data and performed filtering in Ruby instead of SQL.

How did you approach the change?

WorkshopIdeasController:

  • Changed includes to joins for Category queries since we only need pluck(:name) for age ranges
  • Updated WHERE clause from string-based to hash-based: where(category_types: { name: "AgeRange" })
  • Optimized @categories_grouped by using joins with SQL-level filtering via .where(category_types: { published: true })
  • Removed Ruby-level .select filtering that was checking after data was already loaded
  • Simplified .sort_by to avoid safe navigation operators since nil types are filtered at SQL level

StoryIdeasController:

  • Applied the new ordered_by_name scope for consistent user sorting with proper SQL joins

WorkshopVariationIdeasController:

  • Added .distinct to prevent duplicate records from .or() query
  • Applied the new ordered_by_name scope for consistent user sorting

User Model:

  • Created a new ordered_by_name scope that uses left_joins(:person) and COALESCE logic to prioritize person record names when available, falling back to user table fields
  • This provides consistent, reusable sorting behavior across all controllers

Anything else to add?

Benefits:

  • Reduced N+1 queries by using joins instead of includes when full objects aren't needed
  • SQL-level filtering reduces memory usage compared to loading all records and filtering in Ruby
  • The new ordered_by_name scope ensures consistent user sorting across controllers and is easily testable

Quality Checks:

  • ✅ All syntax checks passed
  • ✅ Code review completed and feedback addressed
  • ✅ Security scan (CodeQL) passed with 0 vulnerabilities
  • ✅ Follows Rails best practices (using Arel.sql, proper joins, scopes)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
@maebeale
Copy link
Collaborator

@copilot finish what you started

Copilot AI and others added 3 commits February 15, 2026 21:29
…ance

Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
…sistency

Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot finish what you started

Done! Completed all performance optimizations:

  1. WorkshopIdeasController: Optimized Category queries using joins instead of includes with SQL-level filtering (commit 5a97e3b)
  2. StoryIdeasController: Fixed User ordering with proper joins and new ordered_by_name scope (commit 5a97e3b)
  3. WorkshopVariationIdeasController: Added .distinct and consistent user ordering (commit 5a97e3b)
  4. User model: Added reusable ordered_by_name scope for consistent sorting across controllers (commit e050261)

All changes reduce database queries and memory usage by filtering/sorting at the SQL level instead of in Ruby. Code review and security checks passed with no vulnerabilities.

Copilot AI changed the title [WIP] Improve performance of workshop idea forms Improve performance of workshop idea forms Feb 15, 2026
@maebeale maebeale marked this pull request as ready for review February 15, 2026 21:45
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