<p dir="auto">Normally all the models are loaded automatically, but when the database is offline the app/models directory is ignored. The community model can still be used offline, since it doesn't depend on activerecord.</p>
<p dir="auto"><span class="issue-keyword tooltipped tooltipped-se" aria-label="This pull request closes issue #3858.">Fixes</span> <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1510427051" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/3858" data-hovercard-type="issue" data-hovercard-url="/openstreetmap/openstreetmap-website/issues/3858/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/issues/3858">#3858</a></p>
<p dir="auto">I'm not 100% happy with this, since I would prefer to keep the mechanics of the database_offline and file loading confined to one place (i.e. <code class="notranslate">config/application.rb</code>). But I couldn't find a way to deal with the eager_loading on a file-by-file basis, so that we could say "ignore all the files in this directory, but then for this one file..."</p>
<div class="highlight highlight-source-ruby notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# Don't eager load models when the database is offline
if Settings.status == "database_offline"
  config.paths["app/models"].skip_eager_load!
  config.paths["app/models/community.rb"].eager_load!  # <- yeah that doesn't work
  config.paths.add("app/models/community.rb", :eager_load => true)# <- yeah that doesn't work either
end"><pre class="notranslate"><span class="pl-c"># Don't eager load models when the database is offline</span>
<span class="pl-k">if</span> <span class="pl-v">Settings</span><span class="pl-kos">.</span><span class="pl-en">status</span> == <span class="pl-s">"database_offline"</span>
  <span class="pl-en">config</span><span class="pl-kos">.</span><span class="pl-en">paths</span><span class="pl-kos">[</span><span class="pl-s">"app/models"</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">skip_eager_load!</span>
  <span class="pl-en">config</span><span class="pl-kos">.</span><span class="pl-en">paths</span><span class="pl-kos">[</span><span class="pl-s">"app/models/community.rb"</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">eager_load!</span>  <span class="pl-c"># <- yeah that doesn't work</span>
  <span class="pl-en">config</span><span class="pl-kos">.</span><span class="pl-en">paths</span><span class="pl-kos">.</span><span class="pl-en">add</span><span class="pl-kos">(</span><span class="pl-s">"app/models/community.rb"</span><span class="pl-kos">,</span> <span class="pl-pds">:eager_load</span> <span class="pl-c1">=></span> <span class="pl-c1">true</span><span class="pl-kos">)</span><span class="pl-c"># <- yeah that doesn't work either</span>
<span class="pl-k">end</span></pre></div>
<p dir="auto">I'm also not 100% on the subtleties of "eager load" vs "auto load" here. I would have thought that marking the directory as not eligible for eager loading would mean that in production they aren't loaded during application boot, but were still auto loaded when required, and in any case were auto loaded during development. But the failure to auto-load from app/models when they are marked as "skip_eager_load!" suggests that I don't fully understand how auto_load and eager_load interact.</p>
<p dir="auto">So based on that, adding the <code class="notranslate">require_relative</code> to the code run from the initializer a relatively nonintrusive way to fix this.</p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/openstreetmap/openstreetmap-website/pull/3861'>https://github.com/openstreetmap/openstreetmap-website/pull/3861</a></p>

<h4>Commit Summary</h4>
<ul>
  <li><a href="https://github.com/openstreetmap/openstreetmap-website/pull/3861/commits/d4e394c7b0a58d1ff4a2d457c20dc5176247e2c6" class="commit-link">d4e394c</a>  Load the community model when the database is offline</li>
</ul>

<h4 style="display: inline-block">File Changes </h4> <p style="display: inline-block">(<a href="https://github.com/openstreetmap/openstreetmap-website/pull/3861/files">1 file</a>)</p>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/3861/files#diff-cc98efe2e7f0566dbd0a2d75e104abcfe5cd7d5d97e370b27da4791d04ed5237">lib/osm_community_index.rb</a>
    (2)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/3861.patch'>https://github.com/openstreetmap/openstreetmap-website/pull/3861.patch</a></li>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/3861.diff'>https://github.com/openstreetmap/openstreetmap-website/pull/3861.diff</a></li>
</ul>

<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/pull/3861">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLO72ZAMA6JVX6232FLWPQTULANCNFSM6AAAAAATLGMSPM">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLK4AH4RMBZN6DD5LLDWPQTULA5CNFSM6AAAAAATLGMSPOWGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHFUKSWWI.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/pull/3861</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/pull/3861",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/3861",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>