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

AR 5.2.1 and 5.2.2 transitive polymorphic association results in INNER JOIN which breaks "or" searches #996

Closed
dholdren opened this issue Jan 17, 2019 · 4 comments · Fixed by #1004

Comments

@dholdren
Copy link

dholdren commented Jan 17, 2019

Problem

When searching a model's polymorphic association's association's field in Rails 5.2 ransack generates a INNER JOIN which causes or type searches to break
(i.e. a Note's notable Article's comment's body)

Working combination: Rails 5.0, ransack 2.1.1 (latest):

This generates LEFT OUTER JOINs for everything

➜  ransack git:(master) rake console
[1] pry(main)> puts Note.ransack("notable_of_Article_type_comments_body_cont" => "First").result.to_sql
SELECT "notes".* FROM "notes" 
LEFT OUTER JOIN "articles" ON "articles"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Article' AND ('default_scope' = 'default_scope') 
LEFT OUTER JOIN "comments" ON "comments"."article_id" = "articles"."id" 
WHERE ("comments"."body" LIKE '%First%')
=> nil

[2] pry(main)> puts Note.ransack("notable_of_Person_type_name_or_notable_of_Article_type_comments_body_cont" => "First").result.to_sql
SELECT "notes".* FROM "notes" 
LEFT OUTER JOIN "people" ON "people"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Person' 
LEFT OUTER JOIN "articles" ON "articles"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Article' AND ('default_scope' = 'default_scope') 
LEFT OUTER JOIN "comments" ON "comments"."article_id" = "articles"."id" 
WHERE ("people"."name" LIKE '%First%' OR "comments"."body" LIKE '%First%')
=> nil

Broken combination Rails 5.2.2 (also 5.2.1), ransack 2.1.1 (latest)

This generates an INNER JOIN for the comments table so it ignores any other tables we chose to search

ransack git:(master) RAILS=v5.2.2 rake console
[1] pry(main)> puts Note.ransack("notable_of_Article_type_comments_body_cont" => "First").result.to_sql
SELECT "notes".* FROM "notes" 
LEFT OUTER JOIN "articles" ON "articles"."id" = "notes"."notable_id" AND ('default_scope' = 'default_scope') 
INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" 
WHERE "comments"."body" LIKE '%First%'
=> nil

[2] pry(main)> puts Note.ransack("notable_of_Person_type_name_or_notable_of_Article_type_comments_body_cont" => "First").result.to_sql
SELECT "notes".* FROM "notes" 
LEFT OUTER JOIN "people" ON "people"."id" = "notes"."notable_id" 
LEFT OUTER JOIN "articles" ON "articles"."id" = "notes"."notable_id" AND ('default_scope' = 'default_scope') 
INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" 
WHERE ("people"."name" LIKE '%First%' OR "comments"."body" LIKE '%First%')
=> nil

Additional bug

You can see in the Rails 5.2.2 example, that the polymorphic associations LEFT OUTER JOIN aren't limited by notable_type. This causes other issues. This was reported in #902

@dholdren dholdren changed the title AR 5.2 transitive polymorphic association results in INNER JOIN which breaks "or" searches AR 5.2.2 transitive polymorphic association results in INNER JOIN which breaks "or" searches Jan 21, 2019
@dholdren dholdren changed the title AR 5.2.2 transitive polymorphic association results in INNER JOIN which breaks "or" searches AR 5.2.1 and 5.2.2 transitive polymorphic association results in INNER JOIN which breaks "or" searches Jan 21, 2019
@dholdren
Copy link
Author

dholdren commented Feb 7, 2019

@gregmolnar Is anyone actively looking into this or #955 ? (sorry for the dup)

Also the separate, but related #902

These are show-stopper bugs for Rails 5.2.1+, polymorphic relationship searches are broken

@gregmolnar
Copy link
Member

@dholdren I am struggling with time recently and as far as I know others are not looking into this either. If all goes well, I should be able to look into it this weekend/early next week.

@gregmolnar
Copy link
Member

@dholdren Can you verify that this PR fixes your issue? #1004

@dholdren
Copy link
Author

dholdren commented Feb 12, 2019

@dholdren Can you verify that this PR fixes your issue? #1004

@gregmolnar Yes it looks good. LEFT OUTER joins, and they now have the polymorphic constraint. thanks!

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 a pull request may close this issue.

2 participants