diff --git a/legacy_promotions/app/models/spree/promotion.rb b/legacy_promotions/app/models/spree/promotion.rb index 0b81e554b4..66c234fb96 100644 --- a/legacy_promotions/app/models/spree/promotion.rb +++ b/legacy_promotions/app/models/spree/promotion.rb @@ -240,6 +240,7 @@ def blacklisted?(promotable) when Spree::LineItem !promotable.variant.product.promotionable? when Spree::Order + ActiveRecord::Associations::Preloader.new(records: promotable.line_items, associations: {variant: :product}).call promotable.line_items.any? { |line_item| !line_item.variant.product.promotionable? } end end diff --git a/legacy_promotions/spec/models/spree/promotion_spec.rb b/legacy_promotions/spec/models/spree/promotion_spec.rb index 441d4cab20..846dd2c526 100644 --- a/legacy_promotions/spec/models/spree/promotion_spec.rb +++ b/legacy_promotions/spec/models/spree/promotion_spec.rb @@ -714,6 +714,17 @@ it { is_expected.to be true } end end + + context "with several line items" do + let(:promotion) { create(:promotion, :with_line_item_adjustment, apply_automatically: true) } + let(:promotable) { create(:order_with_line_items, line_items_count: 3) } + + before { promotable.reload } + + it "loads line item variants in a single query" do + expect { subject }.to make_database_queries(matching: /from .spree_variants..*\bid. IN \(/im, count: 1) + end + end end context "when promotable is a Spree::LineItem" do