使用ActiveRecord选择时,如何排除存储在Postgres中的json部分?

编程入门 行业动态 更新时间:2024-10-24 09:26:55
本文介绍了使用ActiveRecord选择时,如何排除存储在Postgres中的json部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是对此的后续问题:

如何仅选择使用ActiveRecord的JSON(存储在Postgres中)的一部分

说我有一个模型 User ,该模型的类型为 json 的字段为设置。假设该字段大致如下:

Say I have a model User, which has a field of type json called settings. Let's assume that this field looks roughly like this:

{ color: 'red', language: 'English', subitems: { item1: true, item2: 43, item3: ['foo', 'bar', 'baz'] } }

与上述问题的区别在于想知道如何排除json的一部分。所以在这里,我想从设置中选择所有内容,除了:

The difference to the question cited above is that I'd like to know how to exclude part of the json. So here, I want to select everything from settings except:

subitems: { item1: true, item2: 43, item3: ['foo', 'bar', 'baz'] }

推荐答案

在我的情况下, >> User.select( subitem->'item1'AS subitem1, subitem->'item2'AS subitem2)。map(&:attributes)

带有 rails 5

更多推荐

使用ActiveRecord选择时,如何排除存储在Postgres中的json部分?

本文发布于:2023-10-17 10:36:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ActiveRecord   Postgres   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!