From 0e57a76ce6c435cbf021bd61e5d75f0b72bf2b32 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 22 Jun 2025 12:37:28 -0700 Subject: [PATCH] Fix NC style series sort order in search dropdown Oops, I didn't realize that the MySQL function `SUBSTRING_INDEX` always returns the full string if the split delimiter isn't found. This meant that, for series names like "Regal", we read the main name as "Regal" (correct) and the variant name as "Regal" (incorrect). This caused sort order to be incorrect for some series, e.g., - Prismatic Dawn: Regal - Prismatic Dusk: Regal - Prismatic Mirage: Regal - Regal whereas the main series name is meant to be first, and *does* come first in cases like "Festive" where the main name sorts before any of the variant names! In this change, we update the variant name definition to return an empty string. That way, when there's no variant name and it's just the main series, that one sorts to the top of the series variants. --- app/models/alt_style.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 751ca3e8..806ae1a8 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -32,11 +32,13 @@ class AltStyle < ApplicationRecord } scope :by_series_main_name, -> { # The main part of the series name, like "Nostalgic". + # If there's no colon, uses the whole string. order(Arel.sql("SUBSTRING_INDEX(series_name, ': ', -1)")) } scope :by_series_variant_name, -> { # The variant part of the series name, like "Prismatic Cyan". - order(Arel.sql("SUBSTRING_INDEX(series_name, ': ', 1)")) + # If there's no colon, uses an empty string. + order(Arel.sql("SUBSTRING(series_name, 1, LOCATE(': ', series_name) - 1)")) } scope :by_color_name, -> { joins(:color).order(Color.arel_table[:name])