title: How to make this ActiveRecord call more efficient with Rails 6.0.3.3 and Ruby 2.7.1
body: <p>My app has the following models used in this example</p>
  <ul>
  <li>Member</li>
  <li>Prayer Requests</li>
  <li>Prayers</li>
  <li>Journal</li>
  <li>Journal Entries</li>
  </ul>
  <p>What I am trying to do is show the number of prayer_requests and prayers done on the same day as a journal entry. I have tried adding code to the model, to the controller or directly in the view and each time it always needs to do extra calls for each entry when displaying them in the view. So, I have gotten it to work, but on the show page for the journal, it does three db calls just to get to the page (member, profile, and journal entries) and then two additional calls for each entry (prayer_requests and prayers). The requests, prayers, and journal are all connected to the member model and the journal entry is connected through the journal to the member. There has to be a more efficient way to do this with eager loading. Any help on how to change my models or ActiveRecord calls would be appreciated.</p>
  <p><strong>Member Model</strong></p>
  <pre><code>class Member &lt; ApplicationRecord
    has_one :profile, dependent: :destroy
    
    has_one  :prayer_journal
    has_many :journal_entries, through: :prayer_journal
  
    has_many :prayer_requests    
    has_many :prayer_request_shares, foreign_key: &quot;shared_with_id&quot;
    has_many :shared_prayer_requests, through: :prayer_request_shares, source: :prayer_request
  
    has_many :prayers
    
    accepts_nested_attributes_for :profile
  
    def daily_prayer_count(date)
      self.prayers.where(&quot;date(created_at) = ?&quot;, date).count
    end
  
    def daily_request_count(date)
      self.prayer_requests.where(&quot;date(created_at) = ?&quot;, date).count
    end
  end
  </code></pre>
  <p><strong>Prayer Journal Model</strong></p>
  <pre><code>class PrayerJournal &lt; ApplicationRecord
    belongs_to :member
    has_many :journal_entries
  end
  </code></pre>
  <p><strong>Journal Entry Model</strong></p>
  <pre><code>class JournalEntry &lt; ApplicationRecord
    belongs_to :prayer_journal
  
    encrypts :content
  
    validates :content, presence: true  
  
    has_many :prayer_requests, :through =&gt; :member
    has_many :prayer_request_updates, :through =&gt; :prayer_requests
    has_many :prayers, :through =&gt; :member
  
    def request_count(requests)
      requests.where(&quot;date(created_at) = ?&quot;, self.created_at).count
    end
  
    def prayer_count(prayers)
      prayers.where(&quot;date(created_at) = ?&quot;, self.created_at).count
    end
  end
  </code></pre>
  <p><strong>Journal Controller</strong></p>
  <pre><code>class PrayerJournalsController &lt; ApplicationController
    before_action :authenticate_member
  
    def show
      @prayer_journal = current_member.prayer_journal
      @journal_entries = @prayer_journal.journal_entries.group_by_day(format: &quot;%a, %B %d, %Y&quot;, reverse: true) { |u| u.created_at }
    end
  end
  </code></pre>
  <p><strong>Journal Show View</strong></p>
  <pre><code>&lt;% if @journal_entries.any? %&gt;
    &lt;div class=&quot;row justify-content-center&quot;&gt;
      &lt;div class=&quot;col-lg-10&quot;&gt;
        &lt;% @journal_entries.each do |date, entries| %&gt;
          &lt;h2 class=&quot;post-archive-item mb-3&quot;&gt;&lt;%= date %&gt;&lt;/h2&gt;
          &lt;%= pluralize(daily_prayer_count(date), &quot;prayer&quot;) given %&gt;
          &lt;%= pluralize(daily_request_count(date), &quot;requests&quot;) made %&gt;
          &lt;% entries.each do |journal_entry| %&gt;
            &lt;span class=&quot;post-archive-meta&quot;&gt;
              &lt;span class=&quot;post-archive-meta-details&quot;&gt;
                &lt;p class=&quot;post-archive-meta-details-date font-weight-bold mb-1&quot;&gt;
                  &lt;span class=&quot;convert-utc-to-time-zone align-middle&quot; style=&quot;text-decoration: underline;&quot;&gt;&lt;%= journal_entry.created_at %&gt;&lt;/span&gt;
                  &lt;%= link_to &quot;Edit&quot;, edit_journal_entry_path(journal_entry), class: 'badge badge-pill badge-primary' %&gt;
                &lt;/p&gt;
              &lt;/span&gt;
            &lt;/span&gt;
            &lt;%= simple_format(journal_entry.content) %&gt;          
          &lt;% end %&gt;
          &lt;hr class=&quot;my-5&quot; /&gt;
  
        &lt;% end %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;% else %&gt;
    &lt;div class=&quot;text-center&quot;&gt;
      &lt;p class=&quot;lead&quot;&gt;Create your first Journal Entry&lt;/p&gt;
      &lt;%= link_to 'New Journal Entry', new_journal_entry_path, class: &quot;btn btn-primary&quot;, style:&quot;color:white;&quot; %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>