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

Fix issue evaluating polymorphic joins #1077

Conversation

deivid-rodriguez
Copy link
Contributor

Fixes #1039, inspired on @PhilCoggins's investigation in #1039 (comment), but being initially more aggressive.

@PhilCoggins
Copy link
Contributor

@deivid-rodriguez Thank you for taking a stab at this. Luckily I wrote tests wrapping our use cases here, so I was able to pull your branch and run a few of them.

I also initially thought that removing Polyamorous for 5.2 would "just work", but somewhere along the way the condition to constrain notable_type gets dropped. Without that condition, we risk joining in polymorphic records of an unintended type (this happened to us in production).

I can get a failing test by adding this line to https://github.com/deivid-rodriguez/ransack/blob/fix_rails_52_polymorphic_joins/spec/ransack/search_spec.rb#L288

# Expect the query to constrain notable_type
expect(s.to_sql).to match /"notes"."notable_type" = 'Person'/

Failure:

       expected "SELECT \"notes\".* FROM \"notes\" LEFT OUTER JOIN \"people\" ON \"people\".\"id\" = \"notes\".\"notable_id\" WHERE \"people\".\"name\" = 'Ernie'" to match /"notes"."notable_type" = 'Person'/
       Diff:
       @@ -1,2 +1,2 @@
       -/"notes"."notable_type" = 'Person'/
       +"SELECT \"notes\".* FROM \"notes\" LEFT OUTER JOIN \"people\" ON \"people\".\"id\" = \"notes\".\"notable_id\" WHERE \"people\".\"name\" = 'Ernie'"

My monkey-patch in the original issue accounted for this in the first if condition:

      if respond_to?(:polymorphic?) && polymorphic?
        super(table, foreign_table)
        .and(foreign_table[foreign_type].eq(klass.name))

With this patch, the query in the failing test should look like:

SELECT notes
FROM notes
LEFT OUTER JOIN people ON people.id = notes.notable_id
AND notes.notable_type = 'People'

Emphasis on the AND condition in the join. Hopefully this makes sense, LMK if this is unclear.

@deivid-rodriguez
Copy link
Contributor Author

Thank you @PhilCoggins!

So, the code the posted in the issue should be correct, and we also have a regression test for it, right?

Can you create a PR and I'll close this one, I don't want to steal attribution, you did all the work here :)

@PhilCoggins
Copy link
Contributor

@deivid-rodriguez The code posted in the issue works with ActiveRecord v5.2.3, but fails on 5-2 stable, see rails/rails@54de9b1.
That commit unfortunately removes the build_join_constraint method being overridden in my original patch.

I've spent nearly a full day going back and forth between ActiveRecord and Polyamorous and I'm afraid I just can't figure it out 😞. I appreciate you trying to attribute the fix to me, but I really just want someone who already understands the internals of this library to fix it.

@deivid-rodriguez
Copy link
Contributor Author

Ok! I'll try to dig in more and see if we can fix this.

@deivid-rodriguez
Copy link
Contributor Author

I plan to (at least try to) figure this out soon 👍

@scarroll32
Copy link
Member

@deivid-rodriguez based on your latest comment this is not ready to merge? I'm wondering why the CI is passing.

@deivid-rodriguez
Copy link
Contributor Author

No, it's not.

If I recall correctly, CI is passing because the assertion in my spec is too soft (needs to be strenghten by the assertion proposed by @PhilCoggins), and because tests against 5-2-stable and 6-0-stable are currently in the "allowed_failures" section, so failures there are "hidden".

@oboxodo
Copy link

oboxodo commented Nov 27, 2019

Hi guys. I'm sorry to be a PITA. We're trying to upgrade our project from rails 5.2.x to 6.0 and this requires us to upgrade ransack from 2.1.1 to 2.3.0. While doing that, some of our tests are failing, apparently because of the bug this patch might solve.

Has anyone been able to use ransack 2.3.0 with rails 5.2.x and rails 6.0.x successfully without this patch?

@scarroll32
Copy link
Member

@oboxodo please see this comment #1081 (comment)

@deivid-rodriguez
Copy link
Contributor Author

Thanks for fixing this @PhilCoggins!!

@deivid-rodriguez deivid-rodriguez deleted the fix_rails_52_polymorphic_joins branch May 28, 2020 09:04
@kul-p
Copy link

kul-p commented Jun 25, 2021

I have this "ArgumentError: wrong number of arguments (given 3, expected 2)"
from
from /Users/vkul9pr/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/polyamorous-2.3.2/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb:32:in `join_constraints'

Don't sure it's related with one you all discuss
I am using rails 6.1 (not have this issue in rails 6.0)
Hope any one can fix this/ 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 this pull request may close these issues.

undefined method `polymorphic?' for ActiveRecord::Reflection::PolymorphicReflection
5 participants