Skip to content

Commit

Permalink
Handle empty result set in variants_option_values ransacker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rainerdema committed Sep 28, 2023
1 parent 272f62d commit f9e1ddf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f9e1ddf

Please sign in to comment.