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 (ruby 2.7) #1531

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

Comments

@apaokin
Copy link

apaokin commented Oct 7, 2024

I try to upgrade my rails version without migration to ruby 3. That's why I try to use ransack not higher than 4.0.0.
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

@piligarcia
Copy link

Had the same issue and for me was solved upgrading to version 4.2.1

@apaokin apaokin changed the title 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 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 (ruby 2.7) Jan 30, 2025
@apaokin
Copy link
Author

apaokin commented Jan 30, 2025

Had the same issue and for me was solved upgrading to version 4.2.1

Currently I try to upgrade my app without upgrading ruby version (2.7). According to rubygems, ransack higher than 4.0.0 works only under ruby 3 or higher. It seems like such upgrade is not possible without ruby 3.

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