Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active September 1, 2021 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggieb/6011870cf59103bc888d69d90a9642ae to your computer and use it in GitHub Desktop.
Save reggieb/6011870cf59103bc888d69d90a9642ae to your computer and use it in GitHub Desktop.
Sort by array position.
STATUSES = [:foo, :bar, :this, :that]
scope :by_status, -> do
status_strings = STATUSES.join(',')
joins(
"JOIN unnest('{#{status_strings}}'::text[]) WITH ORDINALITY t(status, order_by_status) USING (status)"
).order('order_by_status')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment