Rails ordering not working as expected using UTF-8 string

Rails ordering order does not work according to UTF-8 line:

> University.order('abbr asc').map(&:abbr)
=> ["", "", ""]

It should be

> University.order('abbr asc').map(&:abbr)
=> ["", "", ""]

What am I missing?

Rails 4.1.8 with ruby ​​2.1.5p273 (version 2014-11-13, version 48405) [x86_64-darwin14.0]

+4
source share
2 answers

Most likely this is the problem of sorting using PostgreSQL:

The collation function allows you to specify the sort order and nature of data classification for a column or even for each operation. This makes it easier to limit the impossibility of changing the LC_COLLATE and LC_CTYPE parameters in the database after it is created.

Rails. - :

class FixCollationForAbbr < ActiveRecord::Migration
  def up
    execute 'ALTER TABLE universities ALTER COLUMN abbr TYPE varchar COLLATE "ru_RU";' 
  end
end

, database.yml:

defaults: &defaults
  adapter: postgresql
  encoding: utf8
  collation: ru_RU.utf8
  ctype: ru_RU.utf8

database.yml PostgreSQL:

def create_database(name, options = {})
  options = { encoding: 'utf8' }.merge!(options.symbolize_keys)

  option_string = options.inject("") do |memo, (key, value)|
    memo += case key
    ...snip...
    when :encoding
      " ENCODING = '#{value}'"
    when :collation
      " LC_COLLATE = '#{value}'"
    when :ctype
      " LC_CTYPE = '#{value}'"
    ...snip...
    end
  end

  execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
end
+4
+1

All Articles