Last active
February 26, 2020 08:04
-
-
Save amkisko/369fb758fecf1ebd8450e4e30fbe919f to your computer and use it in GitHub Desktop.
ActiveRecord safe order
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
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