Rails 3 Ruby 1.9.2: UTF-8 characters show garbled in console and view
My database table has a column开发者_开发问答 with utf8_general_ci collation.
The database.yml has encoding: utf8
The config/application.rb has: config.encoding = "utf-8"
When I use mysql command line and directly query the field it shows: 3√5^2 = 5^(2/3); 5^(2/3) = 3√5^2
When I use rails console (or just show in a view) and output the field it shows: 3√5^2 = 5^(2/3); 5^(2/3) = 3√5^2
As you can see the sqrt sign is messed up.
What am I doing wrong?
After a long research I found the solution. It seems like the columns in question were double encoded. They used to have Latin1 collation and were not converted correctly to UTF8.
A proposed solution to change the column to a BLOB and then back to TEXT with UTF8 did not work:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
What did eventually work was:
mysqldump -uuser -ppassword --opt --quote-names --skip-set-charset --default-character-set=latin1 dbname1 table1 > dump.sql
mysql -uuser -ppassword --default-character-set=utf8 dbname1 < dump.sql
精彩评论