Skip to content
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

SQL aliases are not supported when joining relations - at least with PG #1437

Open
Galaxy83 opened this issue Sep 7, 2023 · 1 comment
Open

Comments

@Galaxy83
Copy link

Galaxy83 commented Sep 7, 2023

ActiveRecord(?) adds aliases to queries to handle ambiguity when passing the query as a nested hash:
Son.joins(:father).where(father: { age: [50, 51] }).ransack(father_name_eq: 'John')
throwing:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: invalid reference....

The workaround is to create an SQL query without the alias so Ransack can be joined:
Son.joins(:father).where('fathers.age': [50,51]).ransack(father_name_eq: 'John')

Obviously, this is not a solution.

Need a way to handle aliases.

@amiel
Copy link

amiel commented Feb 1, 2024

Hi, this is not a solution, but another workaround for folks running in to this issue.

It works for me to use the nested hash syntax instead of the string syntax as long as the referenced name is the pluralized table name as opposed to the singular relationship name.

# 💥 exception
Son.joins(:father).where(father: { age: [50, 51] }).ransack(father_name_eq: 'John')

# ✅ works
Son.joins(:father).where(fathers: { age: [50, 51] }).ransack(father_name_eq: 'John')

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

No branches or pull requests

2 participants