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']
}
User.select("settings -> ['color', 'language'] AS modified_settings"), which should return objects, that respond to the methodmodified_settings, which should return{color: 'red', language: 'English'}. Do you know how to select multiple keys at once?keys_to_fetch.map{ |k| "setting -> '#{k}' as setting_#{k}"}.join(',')