我有一个名为'my_models'的表,其中有一个名为'settings'的'json'列。
我也有以下模型:
class MyModels < ActiveRecord::Base
end
MyModels实例的'settings'属性是一个Hash。
是否有可能配置'MyModels'类型转换'设置'的原始列值HashWithIndifferentAccess而不是哈希?
单独序列化在这里不起作用,因为HashWithIndifferentAccess
不响应load
和dump
方法,但是您可以这样做:
class THEModel < ActiveRecord::Base
def my_hash_attribute
read_attribute(:my_hash_attribute).with_indifferent_access
end
end
参见Rails中字段的自定义序列化