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

Add inverse_of to various model associations #3471

Open
gravitystorm opened this issue Feb 23, 2022 · 1 comment
Open

Add inverse_of to various model associations #3471

gravitystorm opened this issue Feb 23, 2022 · 1 comment
Labels
dx Developer Experience

Comments

@gravitystorm
Copy link
Collaborator

Normally Rails can autodetect bi-directional associations. This has two advantages:

  • Saves memory. If you do e.g. issue.reports.first.issue you get the same object back, without having to load issue fresh from the database (or duplicate it from the query cache).
  • Makes validations easier. If you have a new issue, with a new report, then the validation will pass even though report.issue_id is currently blank - rails knows it will be filled in later.

However, Rails can't autodetect this bi-directional association whenever a foreign_key is specified. The solution is to specify the inverse_of on certain relationships where the autodetection fails. Unfortunately for us, we have a lot of explicitly specified foreign_key options!

Rubocop can find these cases for us, without making us specify them on everything. So we should enable Rails/InverseOf (cop docs) and add the inverse relationships.

@faebebin
Copy link
Contributor

Shouldn't this issue be closed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dx Developer Experience
Projects
None yet
Development

No branches or pull requests

2 participants