【发布时间】:2015-03-27 17:52:20
【问题描述】:
在 json 字段中使用 update 的最佳方式是什么。我希望我的 JSON 字段接受新密钥或更新现有密钥,但不覆盖整个字段。 ActiveRecord 在更新已更改的字段方面做得很好,但我不明白如何将其应用于 json 记录中的子字段...
it 'can update settings with a plain object' do
integration = Integration.create(
name: 'Name',
json_settings: {
key1: 1,
key2: 2
}
)
integration.update(
settings: { key2: 2 }
)
// json_settings is now { "key2": 3 } but I want
// { "key1": 1, "key2": 3 }
expect(integration.json_settings['key1']).to eq('1') // fails
end
【问题讨论】:
标签: ruby-on-rails json postgresql activerecord