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

After I've upgraded my rails app from 5.2 to 7.0, ransack v 4.0.0 fails to form queries containing the same table twice #1531

Open
apaokin opened this issue Oct 7, 2024 · 0 comments

Comments

@apaokin
Copy link

apaokin commented Oct 7, 2024

I have User model with the following associations:

  has_many :surveys, class_name: "Sessions::UserSurvey", dependent: :destroy
  has_many :sessions, class_name: "Sessions::Session", through: :surveys 

The code below works fine in rails 5.2, but in rails 7 ransack tries to use aliases without actually creating them in left joins.

relation = User.ransack(sessions_id_in: 3, surveys_state_in: %w[filling]).result(distinct: true)
puts relation.to_sql 
relation.to_a # rails 5.2: ok, rails 7.0:  PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "surveys_users"

Rails 5.2 SQL:

SELECT DISTINCT "users".* FROM "users" 
LEFT OUTER JOIN "sessions_user_surveys" ON "sessions_user_surveys"."user_id" = "users"."id"
LEFT OUTER JOIN "sessions_sessions" ON "sessions_sessions"."id" = "sessions_user_surveys"."session_id"
LEFT OUTER JOIN "sessions_user_surveys" "surveys_users" ON "surveys_users"."user_id" = "users"."id"
WHERE ("sessions_sessions"."id" IN (3) AND "surveys_users"."state" IN ('filling'))

Rails 7 SQL:

SELECT DISTINCT "users".* FROM "users"
LEFT OUTER JOIN "sessions_user_surveys" ON "sessions_user_surveys"."user_id" = "users"."id"
LEFT OUTER JOIN "sessions_sessions" ON "sessions_sessions"."id" = "sessions_user_surveys"."session_id"
WHERE ("sessions_sessions"."id" IN (3) AND "surveys_users"."state" IN ('filling'))

I tried to downgrade ransack to the one used in rails 5 (2.5.0), but the output remained the same.
Gemflie.lock v 5.2
Gemflie.lock v 7.0

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

1 participant