title: Convert #<ActiveRecord::AssociationRelation to #<ActiveRecord::Relation
body: <p>Ok, so I'm getting an error breaking my code when the @products array is <code>#&lt;ActiveRecord::AssociationRelation</code> when it's <code>#&lt;ActiveRecord::Relation</code> everything works fine</p>
  <pre><code>    if params[:collection_id] &amp;&amp; params[:collection_id] != &quot;&quot;
        @products = Collection.find_by(id: params[:collection_id]).products.not_hidden
      else
        @products = Product.for_company(@current_company).not_hidden.where(category_id: @category.subtree.visible.ids)
      end
      puts(&quot;===============&quot;)
      puts @products.inspect
      puts(&quot;===============&quot;)
  </code></pre>
  <p>First case, which causes the error, returns</p>
  <pre><code>#&lt;ActiveRecord::AssociationRelation [#&lt;Product id: 29, bar_code: &quot;&quot;, deleted: false, price: 3242.0, discounted_price: 52.0, category_id: 15, tags: &quot;&quot;, created_at: &quot;2020-12-14 12:29:04&quot;, updated_at: &quot;2020-12-17 14:45:26&quot;, final_price: 52.0, status: &quot;visible&quot;, sku: &quot;&quot;, barcode: &quot;&quot;, priority: 100, min_quantity: 1, max_quantity: 100, add_shipping_fees: true, weight: 0.0, inventory: {&quot;jklj&quot;=&gt;{&quot;sku&quot;=&gt;&quot;&quot;, &quot;barcode&quot;=&gt;&quot;&quot;, &quot;quantity&quot;=&gt;&quot;13&quot;}, &quot;ojio9&quot;=&gt;{&quot;sku&quot;=&gt;&quot;&quot;, &quot;barcode&quot;=&gt;&quot;&quot;, &quot;quantity&quot;=&gt;&quot;555&quot;}}, add_to_inventory: true, is_translated: true, requires_preparing: false, name: &quot;product englishh&quot;, description: &quot;&quot;&gt;]&gt;
  </code></pre>
  <p>Second case, which works fine, returns</p>
  <pre><code> #&lt;ActiveRecord::Relation [#&lt;Product id: 28, bar_code: &quot;&quot;, deleted: false, price: 21312.0, discounted_price: 23.0, category_id: 16, tags: &quot;&quot;, created_at: &quot;2020-12-10 12:03:37&quot;, updated_at: &quot;2020-12-14 13:45:17&quot;, final_price: 23.0, status: &quot;visible&quot;, sku: &quot;&quot;, barcode: &quot;&quot;, priority: 100, min_quantity: 1, max_quantity: 234, add_shipping_fees: true, weight: 0.0, inventory: {&quot;ewqeqwr&quot;=&gt;{&quot;sku&quot;=&gt;&quot;&quot;, &quot;barcode&quot;=&gt;&quot;&quot;, &quot;quantity&quot;=&gt;&quot;0&quot;}}, add_to_inventory: true, is_translated: true, requires_preparing: false, name: &quot;ewqeqwr&quot;, description: &quot;&lt;p&gt;qweqw&lt;/p&gt;\r\n&quot;&gt;,]&gt;
  </code></pre>
  <p>So you can see that the only difference is that the one breaking is AssociationRelation instead of Relation, how can I convert it to Relation?</p>
  <p>The error is the following:</p>
  <pre><code>PG::InvalidParameterValue: ERROR:  cannot call jsonb_each on a non-object
  : SELECT  DISTINCT &quot;products&quot;.&quot;priority&quot; AS alias_0, &quot;products&quot;.&quot;id&quot; AS alias_1, &quot;products&quot;.&quot;id&quot; FROM &quot;products&quot; LEFT OUTER JOIN &quot;product_translations&quot; ON &quot;product_translations&quot;.&quot;product_id&quot; = &quot;products&quot;.&quot;id&quot; INNER JOIN &quot;collection_products&quot; ON &quot;products&quot;.&quot;id&quot; = &quot;collection_products&quot;.&quot;product_id&quot; WHERE &quot;products&quot;.&quot;deleted&quot; = $1 AND &quot;collection_products&quot;.&quot;collection_id&quot; = $2 AND &quot;products&quot;.&quot;status&quot; IN ($3, $4) AND &quot;products&quot;.&quot;status&quot; = $5 AND (add_to_inventory =FALSE OR (add_to_inventory =TRUE AND inventory !='{}' )) AND NOT (ARRAY(select distinct (jsonb_each(inventory)).value-&gt;&gt;'quantity' as integer)='{0}') ORDER BY &quot;products&quot;.&quot;priority&quot; ASC, &quot;products&quot;.&quot;id&quot; ASC LIMIT $6 OFFSET $7 excluded from capture: Not configured to send/capture in environment 'development'
  </code></pre>
  <p>The json_b each run happens here</p>
  <pre><code>@products = @products.offset(offset)
             .limit(@limit)
             .where(status: &quot;visible&quot;)
             .where(&quot;add_to_inventory =? OR (add_to_inventory =? AND inventory !=? )&quot;, false, true, &quot;{}&quot;)
             .where.not(&quot;ARRAY(select distinct (jsonb_each(inventory)).value-&gt;&gt;'quantity' as integer)='{0}'&quot;)
  </code></pre>