Skip to content

Instantly share code, notes, and snippets.

@ChengLong
Created March 24, 2014 09:54
Show Gist options
  • Save ChengLong/9737413 to your computer and use it in GitHub Desktop.
Save ChengLong/9737413 to your computer and use it in GitHub Desktop.
Store Emoji in Mysql on Rails

I recently came accross this error while working on a rails app

ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: '\xF0\x9F\x98\x84' for column ...

A bit search shows that this error is famously known. You can see more details from here and here.

In short, this error happens because Mysql uses 3 bytes for utf8 character by default. So when a 4-byte character is sent, it crashes.

Solution: use encoding utf8mb4 and collation utf8mb4_unicode_ci

  adapter: mysql2
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci

If you can, stay away from mysql and use postgresql...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment