模型
class PushNotificationRequest < ApplicationRecord
serialize :details
迁移
class CreateNotificationRequests < ActiveRecord::Migration[5.2]
def change
create_table :notification_requests do |t|
t.references :order, references: :spree_orders, index: false
t.string :key
t.json :details
t.string :type
t.timestamps
end
end
end
在控制台上创建数据
PushNotificationRequest.create(order: Spree::Order.last, details: {a: 2})
MySQL怪异存储
mysql> select * from notification_requests;
+----+----------+------+----------------+-------------------------+-----------+---------------------+---------------------+
| id | order_id | key | details | type | status | created_at | updated_at |
+----+----------+------+----------------+-------------------------+-----------+---------------------+---------------------+
| 7 | 19 | NULL | "---\n:a: 2\n" | PushNotificationRequest | INITIATED | 2019-01-09 13:45:40 | 2019-01-09 13:45:40 |
+----+----------+------+----------------+-------------------------+-----------+---------------------+---------------------+
这个
details
列存储为一些奇怪的字符串,而不是正确的JSON
我使用的是MySQL8.0.12和Rails 5.12
我有什么东西不见了吗?