Alexander Popov
Alexander Popov

Reputation: 24905

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

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']
  }

Upvotes: 1

Views: 678

Answers (1)

Nyein
Nyein

Reputation: 286

in my case,
>>User.select("subitem -> 'item1' AS subitem1", "subitem->'item2' AS subitem2").map(&:attributes)

with rails 5

Upvotes: 2

Related Questions