LoginSignup
6
7

More than 5 years have passed since last update.

RailsのApplication Templateでmysqlのcollation設定を追加

Last updated at Posted at 2014-01-27

Rails - ActiveRecordでデフォルトの照合順序を変更する - Qiita [キータ]に書かれていますが、config/database.ymlでcollationを指定しないとutf8_unicode_ciとして扱われてしまいます。

utf8_general_ciを使いたい場合は、下記のように明示的にcollationを指定する必要があります。

development:
  adapter: mysql2
  encoding: utf8
  collation: utf8_general_ci
  database: foo_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

この設定追加をApplication Templateで行う方法を紹介します。

Module: Thor::Actions — Documentation for wycats/thor (master)を使って、encoding: utf8の行を置換してcollation: utf8_general_ciの行を追加します。

# add mysql collation configs to database.yml
gsub_file 'config/database.yml',
  /^  encoding: utf8$/, "\\0\n  collation: utf8_general_ci"
6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7