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

I get 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 because the line I'm trying to call gsub (which comes directly from the ActiveRecord object field) is ASCII-8BIT . I read several articles, messages and answers that say that this is caused by an error in the encoding of the mysql gsm code and the mysql2 suggestion.

But I am already using mysql2. I tried version 0.2.x and latest version 0.3.7 and will not solve the problem:

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

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

+7
source share
2 answers

Yeah! My petty knowledge strikes again. The problem really was in database.yml:

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

I still used adapter: mysql , so although the mysql2 stone was installed, it was not used. I did not understand that I would have to change his name in database.yml ; I thought this would replace the old mysql gem.

Now we all know! :)

+8
source

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

 development: encoding: utf8 
+1
source

All Articles