mercredi 25 novembre 2020

Find records have count of association > n

class PurchaseOrder < ApplicationRecord
  has_many :purchase_order_materials 
end 
class PurchaseOrderMaterial < ApplicationRecord
  belongs_to :purchase_order
  belongs_to :material
end

I have this query:

PurchaseOrderMaterial
  .includes(:purchase_order, :material)
  .where("purchase_orders.provider_id IS NULL AND purchase_orders.status = ? 
          AND (purchase_orders.is_waiting != ? 
               OR purchase_orders.is_waiting IS NULL)", 
         PurchaseOrder.possibleStatus.first, true)

All i want it's filter results to purchase_order.purchase_order_materials.count > 1. Some way to achieve it?

Aucun commentaire:

Enregistrer un commentaire