I am working on a Ruby on Rails project and I have several tasks with this structure in a Postgresql database :
id: 8,
name: "Task 1",
due_date: Mon, 25 Jul 2022,
finished: true,
reviewed: false,
note: nil,
tags: [{"id"=>6, "name"=>"tag_1"}, {"id"=>7, "name"=>"tag_2"}],
assigned_to: [{"id"=>7, "username"=>"Julia"}, {"id"=>8, "username"=>"Joe"}],
user_id: 5,
group_id: 4,
created_at: Sat, 06 Aug 2022 01:09:31.135203000 UTC +00:00,
updated_at: Sat, 06 Aug 2022 01:09:31.135203000 UTC +00:00,
pending: false>
And I want to query those tags depending on the the tag
id and the assigned_to
id.
The column types are:
t.jsonb "tags", default: [], array: true
t.jsonb "assigned_to", default: [], array: true
I checked this source: Query on Postgres JSON array field in Rails
but couldn't figure out how it works, this is what I get:
Task.where("tags::jsonb @> ?", [{"id": 6}, {"id": 7}].to_json)
Task Load (3.8ms) SELECT "tasks".* FROM "tasks" WHERE (tags::jsonb @> '[{"id":6},{"id":7}]')
Task Load (0.9ms) SELECT "tasks".* FROM "tasks" WHERE (tags::jsonb @> '[{"id":6},{"id":7}]') /* loading for inspect */ LIMIT $1 [["LIMIT", 11]]
=> #<Task::ActiveRecord_Relation:0x4290>
Is there a way to query it? Thanks!