Skip to content

Instantly share code, notes, and snippets.

@yancya
Created August 8, 2019 06:57
Show Gist options
  • Save yancya/ee5a87b8c25502ac0faa2dc3dccd19c3 to your computer and use it in GitHub Desktop.
Save yancya/ee5a87b8c25502ac0faa2dc3dccd19c3 to your computer and use it in GitHub Desktop.
ROOMS = ['a', 'b']
rooms_table = ROOMS.map.with_index { |r, i| "('#{r}',#{i})" }.join(',').yield_self { |s| "(VALUES#{s}) as rooms(room, room_order)" }
Topic.joins("join #{rooms_table} using(room)").order(:room_order)
#=> SELECT "topics".* FROM "topics" join (VALUES('a',0),('b',1)) as rooms(room, room_order) using(room) ORDER BY "room_order" ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment