简体   繁体   English

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

[英]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.

相关问题 如何使用ActiveRecord仅选择存储在Postgres中的json的一部分 - How to select only part of json, stored in Postgres, with ActiveRecord 如何在数据库中存储为文本的json字段中使用Activerecord搜索 - How to search using Activerecord in a json field stored as text in DB Rails 3:在Postgres支持的ActiveRecord中使用json作为列类型时出现迁移错误 - Rails 3: migration error when using json as a column type in an ActiveRecord backed by Postgres 使用as_json时如何干燥Rails / ActiveRecord包含的内容? - How to DRY up Rails/ActiveRecord includes when using as_json? 使用ActiveRecord更新Postgres JSON字段 - Postgres JSON field update with ActiveRecord ActiveRecord :: StatementInvalid(SQLite3 :: ReadOnlyException:使用postgres时 - ActiveRecord::StatementInvalid (SQLite3::ReadOnlyException: when using postgres 保存记录时如何保持数组结构-activerecord / postgres - How to keep array structure when saving record - activerecord/postgres 如何使用activerecord排除值? - How to use activerecord to exclude a value? 如何使用ActiveRecord排除记录集合? - How to exclude a collection of records with ActiveRecord? 如何使用ActiveRecord查询获取架构Postgres的值大小 - How To Take Value Size Of Schema Postgres Using ActiveRecord Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM