Skip to content

Instantly share code, notes, and snippets.

@wowremywang
Created November 3, 2021 15:10
Show Gist options
  • Save wowremywang/7ba8bccf7095fcc4eb452fa9373e507a to your computer and use it in GitHub Desktop.
Save wowremywang/7ba8bccf7095fcc4eb452fa9373e507a to your computer and use it in GitHub Desktop.
ActiveRecord Queries
### 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