I'm in the process of upgrading an old legacy Rails 2.3 app to something more modern and running into an encoding issue. I've read all the existing answers I can find on this issue but I'm still running into problems.
Rails ver: 2.3.17 Ruby ver: 1.9.3p385
My MySQL tables are default charset: utf8
, collation: utf8_general_ci
. Prior to 1.9 I was using the original mysql
gem without incident. After upgrading to 1.9 when it retrieved anything with utf8 characters in it would get this well-documented problem:
ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8)
I switched to the mysql2
gem for it's superior handling and I no longer see exceptions but things are definitely not encoding correctly. For example, what appears in the DB as the string Repoussé
is being rendered by Rails as Repoussé
, “Boat”
appears as “Boatâ€
, etc.
A few more details:
- I see the same results when I use the
ruby-mysql
gem as the driver. - I've added
encoding: utf8
lines to each entry in mydatabase.yml
I've also added the following to my environment.rb
:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
It has occurred to me that I may have some mismatch where latin1 was being written by the old version of the app into the utf8 fields of the database or something, but all of the characters appear correctly when viewed in the mysql
command line client.
Thanks in advance for any advice, much appreciated!
UPDATE: I now believe that the issue is that my utf8 data is being coerced through a binary conversion into latin1 on the way out of the db, I'm just not sure where.
mysql> SELECT CONVERT(CONVERT(name USING BINARY) USING latin1) AS latin1, CONVERT(CONVERT(name USING BINARY) USING utf8) AS utf8 FROM items WHERE id=myid;
+-------------+----------+
| latin1 | utf8 |
+-------------+----------+
| Repoussé | Repoussé |
+-------------+----------+
I have my encoding
set to utf8
in database.yml, any other ideas where this could be coming from?
utf-8
? – Aleksei Matiushkin<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
– Matt SandersLogger.debug
your data from your template, model, whereever. I guess, that’s not amysql
driver corrupting your data. – Aleksei Matiushkin.find
an affected model I can see the encoding issue in the model's fields. – Matt Sanders