Potential fix for one of the issues mentioned in #49

Reduces the number of SQL queries used on group_orders/index and
group_orders/archive, but adds extra  complexity to the code
This commit is contained in:
Florian Lentsch 2020-07-12 10:26:36 +02:00 committed by Patrick Gansterer
parent 602f663245
commit 5eb8abf431
7 changed files with 45 additions and 9 deletions

View file

@ -125,6 +125,20 @@ class Order < ApplicationRecord
self
end
# fetch current Order scope's records and map the current user's GroupOrders in (if any)
# (performance enhancement as opposed to fetching each GroupOrder separately from the view)
def self.ordergroup_group_orders_map(ordergroup)
orders = includes(:supplier)
group_orders = GroupOrder.where(ordergroup_id: ordergroup.id, order_id: orders.map(&:id))
group_orders_hash = Hash[group_orders.collect {|go| [go.order_id, go]}]
orders.map do |order|
{
order: order,
group_order: group_orders_hash[order.id]
}
end
end
# search GroupOrder of given Ordergroup
def group_order(ordergroup)
group_orders.where(:ordergroup_id => ordergroup.id).first