Skip to content

Instantly share code, notes, and snippets.

@amkisko
Last active February 26, 2020 08:04
Show Gist options
  • Save amkisko/369fb758fecf1ebd8450e4e30fbe919f to your computer and use it in GitHub Desktop.
Save amkisko/369fb758fecf1ebd8450e4e30fbe919f to your computer and use it in GitHub Desktop.
ActiveRecord safe order
module ActiveRecord
class Relation
def safe_order(field, direction = "asc")
direction = direction.downcase.to_sym
# NOTE: https://gist.github.com/amkisko/aec1283b9797e94348f89e3a2b934bc0
raise Error::CustomError, code: "QUERY_ORDER_INVALID_DIRECTION" unless %i[asc desc].include?(direction)
# field = connection.quote_table_name(field)
order(field => direction)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment