Rails find_by using relationships
If you want to find records via a relationship you can do it easily:
product = Product.joins(:variants).find_by(variants: { sku: 'SKU' }
Instead of:
product = Variant.find_by(sku: 'SKU')&.product
If you want to find records via a relationship you can do it easily:
product = Product.joins(:variants).find_by(variants: { sku: 'SKU' }
Instead of:
product = Variant.find_by(sku: 'SKU')&.product