title: How to query JSONB field with IN operator in rails - active record
body: <p>I have a jsonb column called <code>lms_data</code> with a hash data-structure inside. I am trying to find elements that match an array of ids. This query works and returns the correct result :</p>
  <pre><code>CoursesProgram
  .joins(:course)
  .where(program_id: 12)
  .where(
  &quot;courses.lms_data-&gt;&gt;'_id' IN ('604d26cadb238f542f2fa', '604541eb0ff9d7b28828c')&quot;)
  </code></pre>
  <p>SQL LOG :</p>
  <pre><code>  CoursesProgram Load (0.5ms)  SELECT &quot;courses_programs&quot;.* FROM &quot;courses_programs&quot; INNER JOIN &quot;courses&quot; ON &quot;courses&quot;.&quot;id&quot; = &quot;courses_programs&quot;.&quot;course_id&quot; WHERE &quot;courses_programs&quot;.&quot;program_id&quot; = $1 AND (courses.lms_data-&gt;&gt;'_id' IN ('604d26cadb61e238f542f2fa', '604541eb0ff9d8387b28828c'))  [[&quot;program_id&quot;, 12]
  </code></pre>
  <p>However when I try to pass a variable as the array of ids :</p>
  <pre><code>CoursesProgram
  .joins(:course)
  .where(program_id: 12)
  .where(
    &quot;courses.lms_data-&gt;'_id' IN (?)&quot;,
  [&quot;604d26cadb61e238f542f2fa&quot;, &quot;604541eb0ff9d8387b28828c&quot;])
  </code></pre>
  <p>I dont get any results and I get two queries performed in the logs...</p>
  <pre><code> CoursesProgram Load (16.6ms)  SELECT &quot;courses_programs&quot;.* FROM &quot;courses_programs&quot; INNER JOIN &quot;courses&quot; ON &quot;courses&quot;.&quot;id&quot; = &quot;courses_programs&quot;.&quot;course_id&quot; WHERE &quot;courses_programs&quot;.&quot;program_id&quot; = $1 AND (courses.lms_data-&gt;'_id' IN ('604d26cadb61e238f542f2fa','604541eb0ff9d8387b28828c'))  [[&quot;program_id&quot;, 12]]
    CoursesProgram Load (0.8ms)  SELECT &quot;courses_programs&quot;.* FROM &quot;courses_programs&quot; INNER JOIN &quot;courses&quot; ON &quot;courses&quot;.&quot;id&quot; = &quot;courses_programs&quot;.&quot;course_id&quot; WHERE &quot;courses_programs&quot;.&quot;program_id&quot; = $1 AND (courses.lms_data-&gt;'_id' IN ('604d26cadb61e238f542f2fa','604541eb0ff9d8387b28828c')) LIMIT $2  [[&quot;program_id&quot;, 12], [&quot;LIMIT&quot;, 11]]
  </code></pre>
  <p>I cannot wrapp my head around this one.
  The queries perform in both cases seem to be the same. Why is one working and the other one not ? and why in the second case is the query performed twice ?</p>