Mysql2 gem, Rails 3.0.3 and "incompatible character encodings"

I am using Rails 3.0.3 and I changed the mysql adapter from ruby-mysql to mysql2, but now I have the following error:

incompatible character encodings: ASCII-8BIT and UTF-8

I read everywhere about it, but I can't fix it.

application.rb:

config.encoding = "utf-8"

database.yml:

development:   
  adapter: mysql2
  encoding: utf8
  database: rails3_development
  username: root
  password:
  host: localhost

Gems:

specs:
  abstract (1.0.0)
  actionmailer (3.0.3)
  actionpack (3.0.3)
  activemodel (3.0.3)
  activerecord (3.0.3)
  activeresource (3.0.3)
  activesupport (3.0.3)
  arel (2.0.7)
  bcrypt-ruby (2.1.4)
  builder (2.1.2)
  erubis (2.6.6)
  i18n (0.5.0)
  jquery-rails (0.2.6)
  mail (2.2.15)
  mime-types (1.16)
  **mysql2 (0.2.6)
  orm_adapter (0.0.4)
  paperclip (2.3.8)
  polyglot (0.3.1)
  rack (1.2.1)
  rack-mount (0.6.13)
  rack-test (0.5.7)
  rails (3.0.3)
  railties (3.0.3)
  rake (0.8.7)
  thor (0.14.6)
  treetop (1.4.9)
  tzinfo (0.3.24)
  warden (1.0.3)
  will_paginate (3.0.pre2)
+3
source share
1 answer

I have a similar problem: varchar field with sorting utf8_bin, having ASCII-8BIT encoding.

The problem is gem mysql2, not Rails or mysql settings, at least in my case, because this does not happen with ruby-mysql stone.

Please check if the problem persists when switching to ruby-mysql.

, irb ruby ​​1.9.2, :

require 'mysql2'
c = Mysql2::Client.new(host: "localhost", username: "root", database: 'd')
c.query("select word from t where word = 'a'").to_a[0]["word"].encoding
# => #<Encoding:ASCII-8BIT>

mysql, utf8_bin.

gem mysql2 result.c 253 :

if (fields[i].flags & BINARY_FLAG) {
  rb_enc_associate(val, binaryEncoding);
} else ...

, (ASCII-8BIT), - utf8_bin... , , , , , , , .

+3

Source: https://habr.com/ru/post/1776978/


All Articles