How to exclude parts of json, stored in Postgres, when selecting using ActiveRecord?

末鹿安然 提交于 2019-12-12 03:25:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!