I have a class constant in my Rails app for mapping integers from a column to strings:
TYPES = [[1, "Beginner"], [2, "Intermediate"], [3, "Advanced"]]
I can use this to convert the integers to a human-readable strings for instances of the model (e.g., TYPES.to_h[type_id]), but I'd also like to use it to SELECT the human-readable string for large numbers of records at once. I could use a CASE statement:
SELECT *, CASE WHEN type_id = 1 THEN 'Beginner'
WHEN type_id = 2 THEN 'Intermediate'
WHEN type_id = 3 THEN 'Advanced'
END AS type_string FROM users;
However, this becomes very verbose, difficult to manage, and time-consuming as the TYPES array gets even slightly longer. Is there a better, more efficient way to select the type string from the type_id?