Skip to content

Instantly share code, notes, and snippets.

View mrpunkin's full-sized avatar

Bryan Corey mrpunkin

View GitHub Profile
class Order < ActiveRecord::Base
def self.fulfillable_for(product_id)
select("orders.*, COUNT(up.product_id) AS unfulfillable").
joins(:order_items).
joins("LEFT JOIN order_items oi ON oi.order_id = order_items.order_id AND oi.product_id != order_items.product_id").
joins("LEFT JOIN products up ON up.product_id = oi.product_id AND up.available_at > #{ActiveRecord::Base.sanitize(Date.today)}").
where(order_items: { product_id: product_id} ).
group("order_items.order_id").
having("unfulfillable <= 0")
end