Skip to content

Instantly share code, notes, and snippets.

@cpkenn09y
Created February 17, 2017 22:40
Show Gist options
  • Save cpkenn09y/b91266a9df7a9a382795b305cf91a46d to your computer and use it in GitHub Desktop.
Save cpkenn09y/b91266a9df7a9a382795b305cf91a46d to your computer and use it in GitHub Desktop.
where_by_ordered_ids
# Use ActiveRecord to find records based on an array of ids
# works with postgresql
scope :where_by_ordered_ids, ->(ids) {
order = sanitize_sql_array(
["position((',' || id::text || ',') in ?)", ids.join(',') + ',']
)
where(:id => ids).order(order)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment