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?
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! :)
Are you setting the connection encoding correctly in config/database.yml
?
development:
encoding: utf8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With