Posts

Showing posts from July, 2016

Rails 4, MySQL, and Emoji (Mysql2::Error: Incorrect string value: '\xF0\x9F\x8C\x9D')

Step 1

Change encoding from utf8 to utf8mb4
class ConvertTableToUtf8mb4 < ActiveRecord::Migration
  def change
    # For the table that will store unicode execute:
    execute "ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
    # For each column with unicode content execute:
    execute "ALTER TABLE `table_name` CHANGE column_name VARCHAR(226) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
  end
end
Step 2

Change database.yml according to the changes made.

development:
  adapter: mysql2
  database: database_name
  username: username
  password: password
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci