本文介绍了使用ActiveRecord选择时,如何排除存储在Postgres中的json部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是对此的后续问题:
如何仅选择使用ActiveRecord的JSON(存储在Postgres中)的一部分
说我有一个模型 User ,该模型的类型为 json 的字段为设置。假设该字段大致如下:
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'] } }与上述问题的区别在于想知道如何排除json的一部分。所以在这里,我想从设置中选择所有内容,除了:
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( subitem->'item1'AS subitem1, subitem->'item2'AS subitem2)。map(&:attributes)
带有 rails 5
更多推荐
使用ActiveRecord选择时,如何排除存储在Postgres中的json部分?
发布评论