I am trying to retrieve all Products with active Variants. I only want the active variants to be included in product.variants
. To do this I want to apply a join not only on the product IDs but also filter the active variants before applying the join. This is what I have tried (applying the where in both places).
from product in Product,
inner_join: variants in assoc(product, :variants),
on: variants.active == true,
where: variants.active == true
The result of the above is all products that have at least one active variant with product.variants
including all variants (active or not).
How can I write this query so that only active variants are part of product.variants
?