Skip to content

Instantly share code, notes, and snippets.

@yancya
Last active August 8, 2019 07:37
Show Gist options
  • Save yancya/fcaab41d197a0693499d7b40f149594d to your computer and use it in GitHub Desktop.
Save yancya/fcaab41d197a0693499d7b40f149594d to your computer and use it in GitHub Desktop.
ROOMS = ['a', 'b']
rooms_order = ROOMS.map.with_index { |r, i| "WHEN '#{r}' THEN #{i} " }.join.yield_self { |s| "CASE room #{s} END"}
Topic.where(room: ROOMS).order(rooms_order)
#=> SELECT "topics".* FROM "topics" WHERE "topics"."room" IN ('a', 'b') ORDER BY CASE room WHEN 'a' THEN 0 WHEN 'b' THEN 1 END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment