Last active
September 1, 2021 12:31
-
-
Save reggieb/6011870cf59103bc888d69d90a9642ae to your computer and use it in GitHub Desktop.
Sort by array position.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
See https://stackoverflow.com/questions/866465/order-by-the-in-value-list/35456954#35456954