开发者

define character in rails migration

All tables and fields created with Rails migrations have latin1_swedish_ci colla开发者_C百科te. Because my project use russian language, this collation is improper.

Is it possible to use migrations with utf8_general_ci?

P.S. My database.yml:

development:
  adapter: mysql2
  encoding: utf8
  collation: utf8_general_ci
  reconnect: false
  database: my_development
  pool: 5
  username: username
  password: 'heavy'
  host: localhost


Here's an example of specifying the collation in your migration:

create_table :users, :options => 'COLLATE=utf8_general_ci' do |t|
  t.string :email
  t.timestamps
end


Rails doesn't set the collation on the connection (only on the database in a create_database call). The best idea here is to set it yourself in your system's MySQL config file, eg. /etc/my.cnf

I actually recommend that you tell MySQL to ignore the client handshake altogether, it's better that the server tells the client which character set and collation to use. The relevant parts of my MySQL conf look like this:

[server]
character_set_server=utf8
collation_server=utf8_unicode_ci
skip_character_set_client_handshake

If you go this path, just remember to do the same on the server you deploy to. If you don't know where your config file is for MySQL then run this from the command prompt: my_print_defaults | grep -A1 "Default options" That will give you the config files MySQL will look for (in order).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜