No release in over 3 years
Adds methods like create_collation to manage collations in your Ruby on Rials project.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

Active Record – Collations

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
end

See Postgres collation documentation on how to write ICU locale strings.

Setup

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
end

The 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')
)

Development

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