LoginSignup
0
0

More than 3 years have passed since last update.

rails 5.2 mysql 5.7 絵文字対応 utf8mb4

Last updated at Posted at 2018-12-22

rails 5.2 で絵文字を保存できるようにする

Overview

shichimi とかいうパズルゲームを運用している。
スコアーをPOSTする機能がありますが、ユーザー名に絵文字を使いたい。
なので新規機能実装する運びになりました。

my.cnf
[mysqld]
character-set-server=utf8mb4
[client]
default-character-set = utf8mb4                                          

mysql のdatabase の文字コードも変更します。

show create database db;
+------------------+------------------------------------------------------------------------------+
| Database         | Create Database                                                              |
+------------------+------------------------------------------------------------------------------+
| jobs_development | CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+------------------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)

alter database db default character set utf8mb4;

+------------------+------------------------------------------------------------------------------+
| Database         | Create Database                                                              |
+------------------+------------------------------------------------------------------------------+
| jobs_development | CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+------------------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Rails なので、database.yamlも変更します。

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
{
status: 200,
responce: [
{
id: 9,
user_name: "🍣🍣🍣 ",
score: 1509,
icon: "noimage.png",
created_at: "2018-12-22T17:37:14.000Z",
updated_at: "2018-12-22T17:37:14.000Z",
max_number: 896
},
{
id: 7,
user_name: "",
score: 868,
icon: "noimage.png",
created_at: "2018-12-22T04:17:52.000Z",
updated_at: "2018-12-22T04:17:52.000Z",
max_number: 224
},
{
id: 6,
user_name: "っっh",
score: 512,
icon: "E8gwGjI5_normal.jpg",
created_at: "2018-12-22T04:15:07.000Z",
updated_at: "2018-12-22T04:15:07.000Z",
max_number: 224
},
{
id: 3,
user_name: "mag",
score: 86,
icon: "noimage.png",
created_at: "2018-12-22T04:03:50.000Z",
updated_at: "2018-12-22T04:03:50.000Z",
max_number: 56
},
{
id: 4,
user_name: "look",
score: 86,
icon: "noimage.png",
created_at: "2018-12-22T04:07:24.000Z",
updated_at: "2018-12-22T04:07:24.000Z",
max_number: 56
},
{
id: 5,
user_name: "col",
score: 68,
icon: "noimage.png",
created_at: "2018-12-22T04:11:41.000Z",
updated_at: "2018-12-22T04:11:41.000Z",
max_number: 56
},
{
id: 1,
user_name: "kko",
score: 25,
icon: "noimage.png",
created_at: "2018-12-22T03:59:29.000Z",
updated_at: "2018-12-22T03:59:29.000Z",
max_number: 14
},
{
id: 2,
user_name: "magu",
score: 25,
icon: "noimage.png",
created_at: "2018-12-22T04:01:19.000Z",
updated_at: "2018-12-22T04:01:19.000Z",
max_number: 14
},
{
id: 8,
user_name: "🍣",
score: 1,
icon: "noimage.png",
created_at: "2018-12-22T17:27:21.000Z",
updated_at: "2018-12-22T17:27:21.000Z",
max_number: 1
}
]
}

できました。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0