From f9e1ddfd811889fd8cbc13e38b849aa24a543a57 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Thu, 28 Sep 2023 16:27:09 +0200 Subject: [PATCH] Handle empty result set in variants_option_values ransacker Updated the `variants_option_values` ransacker to handle scenarios where there are no matching product IDs. By leveraging the `.presence` method, we ensure that the ransacker returns `nil` instead of an empty array when there are no results. This prevents potential SQL syntax errors across different database systems. --- core/app/models/spree/product.rb | 1 + core/spec/models/spree/product_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb index af9f7330b08..29b0eff2eb9 100644 --- a/core/app/models/spree/product.rb +++ b/core/app/models/spree/product.rb @@ -145,6 +145,7 @@ def find_or_build_master .where(spree_option_values: { id: v }) .distinct .pluck(:id) + .presence } do |parent| parent.table[:id] end diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb index 82a90cdcc62..8075252106f 100644 --- a/core/spec/models/spree/product_spec.rb +++ b/core/spec/models/spree/product_spec.rb @@ -662,6 +662,12 @@ class Extension < Spree::Base expect(result).to contain_exactly(product_1, product_2) end + it "returns no products if there is no match" do + non_existing_option_value_id = 99999 + result = Spree::Product.ransack(variants_option_values_in: [non_existing_option_value_id]).result + expect(result).to be_empty + end + it "returns products that match any of the provided option value IDs" do product_1 = create(:product) product_2 = create(:product)