Fix PostgreSQL compatibility issue with DISTINCT and ORDER BY #1582
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
PostgreSQL requires that when using DISTINCT, all columns in the ORDER BY clause must also be present in the SELECT clause. This causes issues when using Ransack with DISTINCT queries that include sorting.
Solution
This PR adds a new
DistinctSortsProcessor
class that:Changes
lib/ransack/distinct_sorts_processor.rb
- Core processor logiclib/ransack/search.rb
- Integrates the processor into the result methodlib/ransack.rb
- Adds require for the new processorspec/ransack/distinct_sorts_processor_spec.rb
- Comprehensive test coverageTesting
The implementation includes extensive test coverage for:
Compatibility
This change is fully backward compatible and only activates when both DISTINCT and ORDER BY are present in a query.
Closes #429