Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ActiveRecord return fields encoded as ASCII-8BIT even with mysql2 gem?

I have been getting this error in Ruby 1.9, Rails 3.0, ActiveRecord 3.0:

incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)

This is happening because the string on which I'm trying to call gsub (which comes directly from an ActiveRecord object field) has an encoding of ASCII-8BIT. I've read several articles, posts and answers saying that this is caused by the mysql gem encoding things wrong, and suggesting mysql2.

But I am using mysql2 already. I've tried a 0.2.x version and the latest 0.3.7 version and neither solve the problem:

irb> str = Discussion.first.content
=> "Something's wrong with encodings..." 
irb> str.encoding
=> #<Encoding:ASCII-8BIT> 

I've changed the database encoding and the table encoding in MySQL, I've also tried setting the LANG env variable with no luck. Is there anywhere else I can look or to see why I'm getting this wrong encoding?

like image 822
mltsy Avatar asked Oct 24 '11 22:10

mltsy


2 Answers

Aha! My shallowness of knowledge strikes again. The problem was indeed in database.yml:

development:
  encoding: utf8
  adapter: mysql2
  [...]

I was still using adapter: mysql so even though the mysql2 gem was installed it wasn't being used. I didn't realize I'd have to change the name of it in database.yml; I thought it would replace the old mysql gem.

Now we all know! :)

like image 164
mltsy Avatar answered Oct 16 '22 00:10

mltsy


Are you setting the connection encoding correctly in config/database.yml?

development:
  encoding: utf8
like image 42
Leonid Shevtsov Avatar answered Oct 15 '22 23:10

Leonid Shevtsov