[ Rails: ActiveRecord db sort operation case insensitive ] shows how to perform a case-insensitive sort with ActiveRecord.
Rails: ActiveRecord db sort operation case insensitive
Table.order("lower(column) DESC")
The code I am working with requires column name to be represented as a symbol so that ActiveRecord will automatically expand it to "table"."column"
. This is required, because some queries contain a join statement with ambiguous column names.
GitLab CE: app/models/concerns/sortable.rb#L19-20
scope :order_name_asc, -> { reorder(name: :asc) } scope :order_name_desc, -> { reorder(name: :desc) }
The table can't be hard coded into the method, because it is an abstract class used for several different tables.
Is there a way to get the table name like ActiveRecord does?
scope :order_name_asc, -> { reorder(%Q{LOWER("#{???}"."name") ASC}) }
scope :order_name_desc, -> { reorder(%Q{LOWER("#{???}"."name") DESC}) }
Is there a way to use a symbolic column name and LOWER
together and let ActiveRecord expand the table name?
Edit: Fixed typo using backticks instead of double quotes in last example.