Skip to content

Instantly share code, notes, and snippets.

@dapi
Last active December 10, 2019 07:20
Show Gist options
  • Save dapi/63a7adafeb28431d5649 to your computer and use it in GitHub Desktop.
Save dapi/63a7adafeb28431d5649 to your computer and use it in GitHub Desktop.
Ordered find record by intarray in Postgresql with Rails
# Find ordered record for postgresql in Rails
# enable_extension "intarray"
module FindOrdered
extend ActiveSupport::Concern
included do
scope :find_ordered, ->(ids) { where(id: ids).order("idx(ARRAY#{ids}::integer[], id)") }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment