In my Car model I have an array of Components stored in a PostgreSQL JSONB field.
A record looks like
id: 1
name: DeLorean
components: [{"name": "engine", "maker": "doc", "scope": "system"},
{"name": "transmission", "maker": "doc", "scope": "subsystem"}]
How can I query the database to fetch only the Cars having a Component with name = engine and maker = doc?
I have tried different things, among which the following but all without success.
Car.where('components @> ?', {name: 'engine', maker: 'doc'}.to_json)
Car.where("components->>'name' = ? AND components->>'maker' = ?", 'engine', 'doc').#<ActiveRecord::Relation []>.Car.where('components @> ?', '[{"name": "engine", "maker": "doc" }]')