1

I have a few User records with a details json column that look like this:

User.create(details: {accounts: [{name: 'one', id: 1}, {name: 'two', id: 2}])
User.create(details: {accounts: [{name: 'three', id: 3}, {name: 'two', id: 2}])
User.create(details: {accounts: [{name: 'three', id: 3}, {name: 'four', id: 4}])

How do I query for all records where id is 2?

1

1 Answer 1

0

Thanks to Using jsonb (PostgreSQL), how do I retrieve items with a certain value that's saved as an array? I was put on the right track.

The query to match:

User.where("? in (SELECT json_array_elements(details->'accounts')->>'id')", "2")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.