This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |