Skip to content

Fix where with custom primary key for belongs_to association #40815

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

Merged
merged 1 commit into from
Dec 12, 2020

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Dec 11, 2020

It is a regression for #40554.

Fixes #40809.

@kamipo kamipo merged commit 5262ec5 into rails:master Dec 12, 2020
@kamipo kamipo deleted the fix_where_with_custom_primary_key branch December 12, 2020 00:07
kamipo added a commit that referenced this pull request Dec 12, 2020
Fix `where` with custom primary key for belongs_to association
@doits
Copy link
Contributor

doits commented Dec 26, 2020

FYI this created an issue querying for nil polymorphic associations, see #40937

kamipo added a commit to kamipo/rails that referenced this pull request Dec 27, 2020
It is a regression for rails#40815.

If `where` on polymorphic association with non Active Record object,
`associated_table.join_primary_key` is called without `klass` in
`AssociationQueryValue`, it is wrong usage for polymorphic association.

Always use `PolymorphicArrayValue` for polymorphic association to avoid
the issue.

Fixes rails#40937.
kamipo added a commit to kamipo/rails that referenced this pull request Jan 12, 2021
Similar to rails#40809 which is fixed by rails#40815, but it is caused by dbda5fe.

`value.id if value.respond_to?(:id)` is not always correct if primary
key is customized on the belongs_to association.

Use `reflection.join_primary_key` in that case.

Fixes rails#41081.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Querying for associated ActiveRecord objects inside where condition fails when the association uses a non-standard foreign key in Rails 6.1
2 participants