8000 Add support for table name in order by and Sorting by columns from joined table by derysdominguez · Pull Request #157 · xing/rails_cursor_pagination · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for table name in order by and Sorting by columns from joined table #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

derysdominguez
Copy link
@derysdominguez derysdominguez commented Mar 28, 2025

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 all order_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

  • Added an order_column method to prefix order_by columns with the table name.
  • Updated the filtered_and_sorted_relation method in paginator.rb to use order_column instead of @order_field.
  • Queries with ambiguous order_by columns (e.g., created_at) execute without errors.
  • Added support for fully qualified column names in the order_by parameter.
  • Updated the decoded_cursor and cursor_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PG::AmbiguousColumn: ERROR: column reference is ambiguous
1 participant
0