I'm upgrading a Rails 3.2.13 application from Ruby 1.8.7-p370 to Ruby 1.9.3-p385. After upgrading, special characters are garbled in text retrieved from the database. For instance "café" appears as "café". My database is latin1 encoded. I'm using mysql2 (0.3.11) and my database.yml
looks like this:
development:
adapter: mysql2
encoding: latin1
database: my_db
username: root
host: localhost
(The same problem is also happening in the production environment, which has the same database config.)
It appears that when ActiveRecord retrieves text from the database, it decodes it as if it were utf-8, not latin1 (or ISO-8859-1) as I've specified.
To diagnose the problem, I wrote a Ruby script that that uses mysql2 to query the database directly, bypassing ActiveRecord:
require 'rubygems'
require 'mysql2'
client = Mysql2::Client.new(:host => "localhost",
:username => "root",
:database => "food52_development_production",
:encoding => "latin1")
result = client.query('SELECT title FROM recipes WHERE id = 12934')
puts result.first["title"]
The recipe with id 12934 has the word "café" in its title. Running this script in 1.9.3 outputs the correctly decoded text ("café"). If I change the :encoding
option to "utf-8"
, I once again see the garbled text ("café").
I also tried placing a breakpoint in ActiveRecord::ConnectionAdapters
, to see how what encoding configuration Rails was initializing the Mysql2::Client
with. It is being passed :encoding => "latin1"
, as expected.
And yet: somewhere along the line, Rails decides to decode the text as utf-8. How do I get Rails to respect the latin1 encoding configuration I specified? Thanks in advance for your help.