Add support for table name in order by and Sorting by columns from joined table #157
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.
This pull request addresses an issue where using the
order_by
parameter with columns that exist in multiple joined tables (e.g.,created_at
) would result in SQL ambiguity errors. The fix ensures that allorder_by
columns are properly prefixed with their respective table names, avoiding conflicts and ensuring correct query execution.It also adds support sorting by columns from joined tables. Previously, attempting to sort by a column from a joined table would result in SQL errors or missing cursor values. With this update, the
order_by
parameter now accepts fully qualified column names (e.g.,table.column
), and the gem ensures proper handling of such cases.Changes Made
order_column
method to prefixorder_by
columns with the table name.filtered_and_sorted_relation
method inpaginator.rb
to useorder_column
instead of@order_field
.order_by
parameter.decoded_cursor
andcursor_for_record
methods to correctly handle and process fully qualified column names.Why This Change is Necessary
When joining multiple tables in a query, columns with the same name (e.g., created_at) become ambiguous unless explicitly prefixed with their table name. Without this fix, the gem would raise a PG::AmbiguousColumn error.
Also, Sorting by columns from joined tables is a common use case in complex queries. Without this fix, the gem would raise errors like PG::UndefinedColumn or RailsCursorPagination::ParameterError when attempting to sort by such columns.
Related Issues
Resolves #106
This update is backward-compatible and does not introduce breaking changes. It ensures that the gem can handle more complex queries involving joined tables.