I just migrated from Ruby 1.8.7 to Ruby 1.9.2 and I keep stumbling on string encoding headaches. My MySQL database is all utf8, and yet every time I query from it and lookup the string encoding, I get ASCII-9BIT.
ruby-1.9.2-p180 :002 > Artist.find(1043).name.encoding
=> #<Encoding:ASCII-8BIT>
So an artist like Sigur Rós prints out as "Sigur R\xC3\xB3s". Turns out this is causing some problems as my client app expects UTF8 json to be returned by my server (as it's always been the case). A temporary workaround seems to be adding force_encoding("UTF-8")
all over my code but it feels extremely messy especially since Ruby 1.8.7 didn't need any of these.
I tried adding encoding magic comments, Encoding.default_external
to my env file, encoding
parameter to my database.yml file, nothing does it.
How am I supposed to deal with Ruby 1.9.x and string encodings?
--
EDIT: In my other Rails app (that's been Ruby 1.9.2 since the very start), all the MySQL strings seems to be encoded in UTF-8. But the database/tables encoding/charset are exactly the same?!
encoding
param in my database.yml file. All my tables have the encoding/charset set to utf8. – samvermette