Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed: remove group by in `Product#with_option_value_ids` scope (#13752)

Damian Legawiec43cdb9f659c5
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
index efb3dede41..350a94066c 100644
--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -107,14 +107,8 @@ module Spree
preload_associations_lazily.
ransack(ransack_params)
result = @search.result(distinct: collection_distinct?)
- result = apply_collection_sort(result)
pagy_options = { limit: limit, page: page }
- # When custom scopes add select values (e.g. price sorting), pre-compute
- # the count without those selects to avoid Mobility's select_for_count
- # crashing on non-Arel select values.
- if result.select_values.any?
- pagy_options[:count] = result.except(:select, :order).count(:all)
- end
+ result = apply_collection_sort(result)
@pagy, @collection = pagy(result, **pagy_options)
@collection
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
index 3fa90dbc1a..0777d99a6a 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -47,11 +47,6 @@ module Spree
]
end
- # Disable distinct when using custom sort scopes that add computed columns
- def collection_distinct?
- !custom_sort_requested?
- end
-
# Applies sorting from the unified `sort` param.
# Custom values ('price', '-price', 'best_selling') use product-specific scopes.
# Standard Ransack values ('name', '-created_at') are handled by base ResourceController.
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 6b9e0d7171..a7dd3c08a8 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -145,6 +145,14 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(ids).to include(product_with_blue.prefixed_id)
expect(ids).not_to include(product_with_red.prefixed_id)
end
+
+ it 'sorts by price while filtering by option values' do
+ get :index, params: { sort: 'price', q: { with_option_value_ids: [option_value_red.prefixed_id, option_value_blue.prefixed_id] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_red.prefixed_id, product_with_blue.prefixed_id)
+ end
end
end
diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rb
index a000010fb7..561733b2ed 100644
--- a/spree/core/app/models/concerns/spree/product_scopes.rb
+++ b/spree/core/app/models/concerns/spree/product_scopes.rb
@@ -210,9 +210,7 @@ module Spree
return none if actual_ids.empty?
- group("#{Spree::Product.table_name}.id").
- joins(variants: :option_values).
- where(Spree::OptionValue.table_name => { id: actual_ids })
+ joins(variants: :option_values).where(Spree::OptionValue.table_name => { id: actual_ids })
end
# Finds all products which have an option value with the name matching the one given