You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text was updated successfully, but these errors were encountered:
Normally Rails can autodetect bi-directional associations. This has two advantages:
issue.reports.first.issue
you get the same object back, without having to loadissue
fresh from the database (or duplicate it from the query cache).issue
, with a newreport
, then the validation will pass even thoughreport.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 theinverse_of
on certain relationships where the autodetection fails. Unfortunately for us, we have a lot of explicitly specifiedforeign_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.
The text was updated successfully, but these errors were encountered: