<p></p>
<p dir="auto">Normally Rails can autodetect bi-directional associations. This has two advantages:</p>
<ul dir="auto">
<li>Saves memory. If you do e.g. <code>issue.reports.first.issue</code> you get the same object back, without having to load <code>issue</code> fresh from the database (or duplicate it from the query cache).</li>
<li>Makes validations easier. If you have a new <code>issue</code>, with a new <code>report</code>, then the validation will pass even though <code>report.issue_id</code> is currently blank - rails knows it will be filled in later.</li>
</ul>
<p dir="auto">However, <a href="https://guides.rubyonrails.org/association_basics.html#bi-directional-associations" rel="nofollow">Rails can't autodetect this bi-directional association whenever a <code>foreign_key</code> is specified</a>. The solution is to specify the <code>inverse_of</code> on certain relationships where the autodetection fails. Unfortunately for us, we have a lot of explicitly specified <code>foreign_key</code> options!</p>
<p dir="auto">Rubocop can find these cases for us, without making us specify them on everything. So we should enable <a href="https://github.com/openstreetmap/openstreetmap-website/blob/172a71e3f242f74a6eb1f7bb45fb4d5f3ddff5e7/.rubocop.yml#L58-L59">Rails/InverseOf</a> (<a href="https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsinverseof" rel="nofollow">cop docs</a>) and add the inverse relationships.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/issues/3471">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLPBU32ETUY5LGJWH3DU4T6MRANCNFSM5PEXWW2Q">unsubscribe</a>.<br />Triage notifications on the go with GitHub Mobile for <a href="https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675">iOS</a> or <a href="https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub">Android</a>.
<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLMBI65VWBNSSYOPXZDU4T6MRA5CNFSM5PEXWW22YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4RDRCNAQ.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/issues/3471</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/issues/3471",
"url": "https://github.com/openstreetmap/openstreetmap-website/issues/3471",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>