Created
November 3, 2021 15:10
-
-
Save wowremywang/7ba8bccf7095fcc4eb452fa9373e507a to your computer and use it in GitHub Desktop.
ActiveRecord Queries
This file contains 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
### Rails, ActiveRecord, query id in array of ints, keep order of passed array | |
# way 1 | |
User.where(id: ids).sort_by { |u| ids.index(u.id) } | |
# way 2 | |
User.where(id: ids).order("position(id::text in '#{ids.join(',')}')") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment