[英]How to exclude parts of json, stored in Postgres, when selecting using ActiveRecord?
This is a follow up question to this one: 这是对此的后续问题:
How to select only part of json, stored in Postgres, with ActiveRecord 如何使用ActiveRecord仅选择存储在Postgres中的json的一部分
Say I have a model User
, which has a field of type json
called settings
. 假设我有一个User
模型,其中有一个json
类型的字段,称为settings
。 Let's assume that this field looks roughly like this: 假设此字段大致如下所示:
{
color: 'red',
language: 'English',
subitems:
{
item1: true,
item2: 43,
item3: ['foo', 'bar', 'baz']
}
}
The difference to the question cited above is that I'd like to know how to exclude part of the json. 与上述问题的不同之处在于,我想知道如何排除json的一部分。 So here, I want to select everything from settings
except: 所以在这里,我想从settings
选择所有内容,除了:
subitems:
{
item1: true,
item2: 43,
item3: ['foo', 'bar', 'baz']
}
in my case, 就我而言
>>User.select("subitem -> 'item1' AS subitem1", "subitem->'item2' AS subitem2").map(&:attributes)
with rails 5 带护栏5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.