Skip to content

Commit a223dd8

Browse files
committed
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.
1 parent 4c2db35 commit a223dd8

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

core/app/models/spree/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def find_or_build_master
143143
.where(spree_option_values: { id: v })
144144
.distinct
145145
.pluck(:id)
146+
.presence
146147
} do |parent|
147148
parent.table[:id]
148149
end

core/spec/models/spree/product_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,12 @@ class Extension < Spree::Base
662662
expect(result).to contain_exactly(product_1, product_2)
663663
end
664664

665+
it "returns no products if there is no match" do
666+
non_existing_option_value_id = 99999
667+
result = Spree::Product.ransack(variants_option_values_in: [non_existing_option_value_id]).result
668+
expect(result).to be_empty
669+
end
670+
665671
it "returns products that match any of the provided option value IDs" do
666672
product_1 = create(:product)
667673
product_2 = create(:product)

0 commit comments

Comments
 (0)