From 78cf35807a61ce466b2ca47303fce5301550e2a8 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 23 Sep 2019 18:37:21 +0100 Subject: [PATCH] Improve preloading of order query to avoid N+1 queries --- app/controllers/api/orders_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 03d4e8caa4..9db18d88c4 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -26,7 +26,10 @@ module Api end def order - @order ||= Spree::Order.find_by_number!(params[:id]) + @order ||= Spree::Order. + where(number: params[:id]). + includes(line_items: { variant: [:product, :stock_items, :default_price] }). + first! end end end