How to serialize - deserialize params hash object?

梦想的初衷 提交于 2019-12-04 15:42:10

Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work.

class User < ActiveRecord::Base
  serialize :preferences
end

user = User.create(preferences: {background: "black", display: "large"})
User.find(user.id).preferences 
# => {background: "black", display: "large"}

Mori's answer is correct, but if your data type in the model is truly a string, it may not fit. Suggest you use text instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!