我有一个名为“my_models”的表,其中有一个名为“settings”的“json”列。
我还有以下模型:
class MyModels < ActiveRecord::Base
end
“MyModels”实例的“设置”属性是一个哈希。
是否可以将“MyModels”配置为将“settings”的原始列值类型转换为 HashWithIndifferentAccess 而不是 Hash?
最佳答案
因为 HashWithIndifferentAccess
不会同时响应 load
和 dump
方法,所以单独序列化在这里不起作用,但您可以这样做:
class THEModel < ActiveRecord::Base
def my_hash_attribute
read_attribute(:my_hash_attribute).with_indifferent_access
end
end
关于ruby - Postgresql JSON 列作为 HashWithIndifferentAccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389018/