L. *_*een 3 postgresql activerecord ruby-on-rails
因此,我有一个模型Item,该模型具有名为的巨大的postgresql JSON字段properties。在大多数情况下,无需查询或更改price此字段,但是我们存储在此字段中。
我目前正在写一个脚本来更新这个price,但是有只有少数独特prices和数以千计的Items所以为了节省时间,我有一个列表Items的每一个独特的price,我试图做所有的更新:
Item.where(id: items).update_all("properties->>'price' = #{unique_price}")
但这给了我:
syntax error at or near "->>"
有没有一种方法可以使用全部更新来更新postgres JSON字段中的字段?
您需要使用jsonb_set()功能,这是一个示例:
Item.where(id: items).
update_all(
"properties = jsonb_set(properties, '{price}', to_json(#{unique_price}::int)::jsonb)"
)
Run Code Online (Sandbox Code Playgroud)
这将保留所有值并仅更新一个键。
阅读文件
| 归档时间: |
|
| 查看次数: |
1223 次 |
| 最近记录: |