Valentin Mihov's Blog

Random pieces of wisdom about technology

Running Ruby 1.9.1 + Rails + MySQL and unicode characters

If you have tried to develop some rails application on Rails + Ruby 1.9.1 and MySQL database and you are storing non-English characters in your database probably you had a lot of pain with errors about incompatible charsets. This is a known problem and there is even a bug in Rails’ lighthouse for it. There is even a hack which is going around the issue. The solution is not perfect, but it works in most of the cases.

The problem in a nutshell is that the MySQL Ruby lib is not respecting the encoding setting defined in the database.yml file and does not encode the strings, that are pulled from the database in that encoding. Here is an example database.yml, which defines the encoding of the database connection:

      adapter: mysql
      database: gsm_dev
      username: root
      encoding: utf8
Fortunately there is a nice solution I found: use the ruby-mysql lib. It is on [gemcutter]( “ your community gem host”) so you can do:
    gem uninstall mysql && gem install ruby-mysql

This is going to uninstall the buggy mysql lib and install the working ruby-mysql. Hope that helps!

P.S.: I am not sure that this is a production-ready solution, but at least it provides a way to run things without hacks until the MySQL C API is fixed.