This problem started as the common "incompatible character encoding: ASCII-8BIT and UTF-8," but this is not what I ask. Rather, I found this problem to occur because some of the fields in my database are marked as ASCII-8BIT when they are retrieved, while most of them are correctly displayed as UTF-8.
For example, in a table with columns countryand nationality, where both columns in row 16 have the same values (copied and pasted), I get
irb(main):003:0> c = Country.find(16)
irb(main):004:0> puts "#{c.name}, #{c.name.encoding}, #{c.name.bytes.to_a}"
land Islands, UTF-8, [195, 133, 108, 97, 110, 100, 32, 73, 115, 108, 97, 110, 100, 115]
irb(main):005:0> puts "#{c.nationality}, #{c.nationality.encoding}, #{c.nationality.bytes.to_a}"
land Islands, ASCII-8BIT, [195, 133, 108, 97, 110, 100, 32, 73, 115, 108, 97, 110, 100, 115]
Similarly, simple puts namegives land Islands, but for nationalityit gives "\xC3\x85land Islands"- the same bytes, different representations.
The encoding for this column seems constant, regardless of whether the string has non-ascii characters, so this is not just a string problem. That is, all values in are nationalityinterpreted as ascii and all those specified nameas UTF-8.
The problem is not limited to one table, and I did not find a template for which the columns were not recognized correctly.
Here are the settings and environment:
- Rails 3.0.0 on Windows 7 64-bit
- Database adapter: mysql2 and mysql show the same behavior.
- Database.yml includes
encoding: utf8 - application.rb includes
config.encoding = "utf-8" - MySQL database, table and both columns are defined as utf8
- Both columns in MySQL are varchar, 255, allow null
- Rails , , . .
- , ?