RTIndy
RTIndy

Reputation: 148

Ruby - mysql2 driver changing encoding / various utf-8 issues

I have an API running on Sinatra. It queries a mysql databases, and returns data in json or xml format. I'm having a problem with unicode data. If I query the production database from the console, I'll get data correctly:

persönlichen

However, in my API results (or if I were to query the database in irb using the mysql2 gem), I get this:

persönlichen

Everything works swimmingly on my development box, which is confounding my efforts to solve the problem.

I have done everything I can to make sure that the database is utf-8 only (encodings, collations, client and server character sets are all utf-8). I'm using the mysql2 driver, which supposedly forces everything to utf-8. I'm setting :encoding => 'UTF8' on my active record connection.

What am I missing?

Upvotes: 0

Views: 798

Answers (1)

RTIndy
RTIndy

Reputation: 148

I was able to nail the problem down - the data wasn't encoded correctly in the database. I was populating my database using a sql dump file - I added this to the top, and everything worked great:

set names utf8;
create database if not exists `my_db_name` CHARACTER SET utf8 COLLATE utf8_general_ci;

Upvotes: 2

Related Questions