Removing n+1 query from #items_bought_by_user

This commit is contained in:
Rob Harrington
2017-05-12 12:39:36 +10:00
parent 77c8c85775
commit e47e10d267

View File

@@ -253,8 +253,8 @@ class OrderCycle < ActiveRecord::Base
def items_bought_by_user(user, distributor)
# The Spree::Order.complete scope only checks for completed_at date, does not ensure state is "complete"
orders = Spree::Order.complete.where(state: "complete", user_id: user, distributor_id: distributor, order_cycle_id: self)
items = orders.map(&:line_items).flatten
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
items = Spree::LineItem.joins(:order).merge(orders)
items.each { |li| scoper.scope(li.variant) }
end