polyamorous icon indicating copy to clipboard operation
polyamorous copied to clipboard

Regression in polymorphic associations

Open rzane opened this issue 7 years ago • 4 comments

This issue only affects Active Record 5.2.

In Active Record < 5.2, polymorphic joins will include a condition on the _type column.

LEFT OUTER JOIN "people"
ON "people"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Person'

In Active Record 5.2, the condition will not be included:

LEFT OUTER JOIN "people"
ON "people"."id" = "notes"."notable_id"

rzane avatar Mar 30 '18 18:03 rzane

Thanks for the issue @rzane, are you seeing issues under Rails 5.2 with this? The old SQL should continue to work, shouldn't it?

scarroll32 avatar Apr 14 '18 05:04 scarroll32

Yeah, this issue only affects Active Record 5.2. In 5.2, the query will be missing the AND "notes"."notable_type" = 'Person' bit, which is very important for polymorphic associations.

This is definitely an issue, because imagine how that query might behave if the notable table looked like this:

id notable_id notable_type
1 99 Dog
2 99 Person

rzane avatar Apr 14 '18 14:04 rzane

@seanfcarroll any chance of having this issue fixed sometime soon? Currently this is the only issue that prevents me from upgrading my project to Rails 5.2. Thanks.

octavpo avatar May 03 '18 04:05 octavpo

@octavpo You should use Ransack 2 which is not using polyamorous anymore and supports Rails 5.2

gregmolnar avatar Oct 27 '18 05:10 gregmolnar