Allows basic management of ICU collations in Postgres.
class UpdateNaturalDeCollation < ActiveRecord::Migration[8.1]
def change
drop_collation(:natural_de)
create_collation(
:natural_de,
'de-u-kn-true-ks-level1-ka-shifted-kv-symbol'
)
end
endSee Postgres collation documentation on how to write ICU locale strings.
Add activerecord-collations to your dependencies. Include the collation concern into your models to get access to the collate class method.
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
include ActiveRecord::Collation
endThe collate method creates an ARel expression for the column with a certain collation.
scope :ordered, -> { order(collate(:title, "natural_#{I18n.locale}") }
scope :ordered_de, -> { order(collate(:title, :natural_de)) }Alternatively you can build it yourself:
Arel::Nodes::InfixOperation.new(
'COLLATE',
arel_table[:title],
Arel.sql('natural_de')
)Run tests in this repository with rake. Migrations are exercised by running them in the dummy application.
cd test/dummy
rm -f db/schema.rb && RAILS_ENV=test rake db:drop db:create db:migrate