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 disable_joins when join foreign key is not ID #42448

Merged

Conversation

eileencodes
Copy link
Member

@eileencodes eileencodes commented Jun 10, 2021

If you have a has_one through association where the through has a
belongs_to the foreign key won't be id for the joins. This change
ensures we use the correct foreign key when querying with disable joins
turned off. Prior to this change the call to project.lead_developer
would return nil if disable_joins was set.

Project has one lead developer through firm. Project also belongs to
firm. So when we call project.lead_developer it needs to find the
lead developer through the firm. In these cases we don't want to read
the owners id, we want to read the attribute that will give us the
value of the first chain item's join_foreign_key.

@eileencodes eileencodes changed the title Fix disable_joins when foreign key is not ID Fix disable_joins when join foreign key is not ID Jun 10, 2021
@cheshire137
Copy link
Contributor

Thank you for this fix! 🎉

If you have a `has_one through` assocation where the `through` has a
`belongs_to` the foreign key won't be `id` for the joins. This change
ensures we use the correct foreign key when querying with disable joins
turned off. Prior to this change the call to `project.lead_developer`
would return `nil` if `disable_joins` was set.

Project has one lead developer through firm. Project also belongs to
firm. So when we call `project.lead_developer` it needs to find the
lead developer through the firm. In these cases we don't want to read
the owners id, we want to read the attribute that will give us the
value of the first chain item's `join_foreign_key`.
@eileencodes eileencodes force-pushed the fix-has_one-with-has_many-disable_joins branch from bf9d4d6 to 566c403 Compare June 10, 2021 20:30
@eileencodes eileencodes merged commit a466bbf into rails:main Jun 10, 2021
@eileencodes eileencodes deleted the fix-has_one-with-has_many-disable_joins branch June 10, 2021 21:38
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.

None yet

2 participants