问题
This is a follow up question to this one:
How to select only part of json, stored in Postgres, with ActiveRecord
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']
}
}
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']
}
回答1:
in my case, >>User.select("subitem -> 'item1' AS subitem1", "subitem->'item2' AS subitem2").map(&:attributes)
with rails 5
来源:https://stackoverflow.com/questions/35314920/how-to-exclude-parts-of-json-stored-in-postgres-when-selecting-using-activerec