title: What postgres operator should be used for comparing arrays/integer with arrays?
body: <p>I have an <code>Event</code> model with a jsonb column for <code>visibility_constraints</code>. A record usually contains four arrays <code>included_users</code>, <code>included_teams</code>, <code>excluded_users</code> and <code>excluded_teams</code>. An <code>Event</code> has a visibility of public or private.</p>
  
  <p>Also created indices for the four arrays:</p>
  
  <pre class="lang-rb prettyprint-override"><code>CREATE INDEX visibility_constraints_included_teams_on_events ON events USING GIN ((visibility_constraints-&gt;'included_teams'))
  </code></pre>
  
  <p>Create event</p>
  
  <pre class="lang-rb prettyprint-override"><code>Event.create(summary: 'Test', visibility_constraints: { included_users: [1, 2, 3], included_teams: [1, 2], excluded_users: [4, 5, 6], excluded_teams: [3, 4] })
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code>=&gt; #&lt;Event:0x00007fc99d8f9670
   id: 1,
   summary: "Test",
   starts_at: Sat, 11 May 2019 21:00:00 CEST +02:00,
   ends_at: Sat, 11 May 2019 22:00:00 CEST +02:00,
   visibility: "vis_private",
   visibility_constraints: {"included_users"=&gt;[1, 2, 3], "included_teams"=&gt;[1, 2], "excluded_users"=&gt;[4, 5, 6], "excluded_teams"=&gt;[3, 4]}&gt;
  </code></pre>
  
  <p>Now I try to retreive events where the <code>current_user.id</code> is in <code>included_users</code>…</p>
  
  <pre class="lang-rb prettyprint-override"><code>Event.where("visibility_constraints -&gt; 'included_users' ? :user_id", user_id: current_user.id.to_json)
  
  (0.4ms)  SELECT "events".* FROM "events" WHERE (visibility_constraints -&gt; 'included_users' ? '1')
  </code></pre>
  
  <p>… or where <code>current_user.team_ids</code> is in <code>included_teams</code>.</p>
  
  <pre class="lang-rb prettyprint-override"><code>Event.where("visibility_constraints -&gt; 'included_teams' ? :team_ids", team_ids: current_user.team_ids.to_json)
  
   (0.5ms)  SELECT "events".* FROM "events" WHERE (visibility_constraints -&gt; 'included_teams' ? '[3,5,1]')
  </code></pre>
  
  <p>These queries always return nothing so I'm wondering what I might do wrong. Did I forget something somewhere? Use a different operator (Well, I tried but nothing changes)?</p>
  
  <p>Thanks for your help!</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>\d public.events</p>
  
  <pre class="lang-sql prettyprint-override"><code>+------------------------+-----------------------------+------------------------------------------------------+
  | Column                 | Type                        | Modifiers                                            |
  |------------------------+-----------------------------+------------------------------------------------------|
  | id                     | bigint                      |  not null default nextval('events_id_seq'::regclass) |
  | summary                | character varying           |                                                      |
  | description            | text                        |                                                      |
  | starts_at              | timestamp without time zone |                                                      |
  | ends_at                | timestamp without time zone |                                                      |
  | allday                 | boolean                     |                                                      |
  | created_at             | timestamp without time zone |  not null                                            |
  | updated_at             | timestamp without time zone |  not null                                            |
  | aasm_state             | character varying           |                                                      |
  | visibility             | visibility                  |  default 'vis_private'::visibility                   |
  | stage_id               | bigint                      |                                                      |
  | visibility_constraints | jsonb                       |  not null default '{}'::jsonb                        |
  +------------------------+-----------------------------+------------------------------------------------------+
  Indexes:
      "events_pkey" PRIMARY KEY, btree (id)
      "index_events_on_stage_id" btree (stage_id)
      "index_events_on_visibility_constraints" gin (visibility_constraints)
      "visibility_constraints_included_teams_on_events" gin ((visibility_constraints -&gt; 'included_teams'::text))
      "visibility_constraints_included_users_on_events" gin ((visibility_constraints -&gt; 'included_users'::text))
  Foreign-key constraints:
      "fk_rails_cb489a462e" FOREIGN KEY (stage_id) REFERENCES stages(id)
  Referenced by:
      TABLE "availabilities" CONSTRAINT "fk_rails_6d29de0e36" FOREIGN KEY (event_id) REFERENCES events(id)
      TABLE "assignments" CONSTRAINT "fk_rails_6a5328b71e" FOREIGN KEY (event_id) REFERENCES events(id)
      TABLE "resource_allocations" CONSTRAINT "fk_rails_2958f606d4" FOREIGN KEY (event_id) REFERENCES events(id)
  
  Time: 0.048s
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 2</strong></p>
  
  <p><code>SELECT * FROM events</code></p>
  
  <pre><code>+------+-----------+----------------------------+----------------------------+-----------------------------------------------------------------------------------------------------------------+
  | id   | summary   | created_at                 | updated_at                 | visibility_constraints                                                                                          |
  |------+-----------+----------------------------+----------------------------+-----------------------------------------------------------------------------------------------------------------|
  | 3    | event 3   | 2019-05-15 00:24:50.699237 | 2019-05-15 00:24:50.699237 | "{\"included_users\":[1,2,3],\"included_teams\":[1,2,3],\"excluded_users\":[4,5,6],\"excluded_teams\":[4,5,6]}" |
  | 4    | event 4   | 2019-05-15 00:24:55.487215 | 2019-05-15 00:24:55.487215 | "{\"included_users\":[1,2,3],\"included_teams\":[1,2,3],\"excluded_users\":[4,5,6],\"excluded_teams\":[4,5,6]}" |
  | 5    | event 5   | 2019-05-15 00:26:15.872433 | 2019-05-15 00:26:15.872433 | "{\"included_users\":[2,3],\"included_teams\":[],\"excluded_users\":[],\"excluded_teams\":[]}"                  |
  +------+-----------+----------------------------+----------------------------+-----------------------------------------------------------------------------------------------------------------+
  </code></pre>