💨

投稿数の記録

2023/08/17に公開

はじめに

今日はカレンダーページに投稿数を比較できる表を作成しました。
完成イメージ↓

前日の投稿数と今日の投稿数は制作中のサイトには必要ないので省きました。
説明には表記してあります。

model

投稿関連のモデルに以下のように記述。

post_record.rb
  #今日
  scope :created_today, -> { where(start_time: Time.current.all_day) }
  #昨日
  scope :created_yesterday, -> { where(start_time: Time.zone.yesterday.all_day) }
  #今週
  scope :created_this_week, -> { where(start_time: Time.current.all_week) }
  #先週
  scope :created_last_week, -> { where(start_time: Time.current.last_week.all_week) }
  #今月
  scope :created_this_month, -> { where(start_time: Time.current.all_month) }
  #先月
  scope :created_last_month, -> { where(start_time: Time.current.last_month.all_month) }
scopeメソッドについて

モデルのスコープ機能とは、モデル側であらかじめ特定の条件式に対して名前をつけて定義し、その名前でメソッドの様に条件式を呼び出すことが出来る仕組みのことです。

scope :スコープの名前, -> { 条件式 }

controller

def calendar
    @post_records = current_user.post_records.all
    @today_post_record = @post_records.created_today
    @yesterday_post_record = @post_records.created_yesterday
    @this_week_post_record = @post_records.created_this_week
    @last_week_post_record = @post_records.created_last_week
    @this_month_post_record = @post_records.created_this_month
    @last_month_post_record = @post_records.created_last_month
  end

モデルで定めたscopeをここで使用する。
@post_recordsにログイン中のユーザーの投稿一覧が入っていて、その中のモデルに沿った投稿を抽出しているイメージ。

view

<div>
    <h4>トレーニング数の比較</h4>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>今日のトレーニング数</th>
          <th>昨日のトレーニング数</th>
          <th>前日比</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><%= @today_post_record.count %></td>
          <td><%= @yesterday_post_record.count %></td>
        <% if @yesterday_post_record.count == 0 %>
          <td>前日のトレーニング数が0のため計算できません</td>
        <% else %>
          <td>
            <% @the_day_before = @today_post_record.count / @yesterday_post_record.count.to_f %>
            <%= (@the_day_before * 100).round %>%
          </td>
        <% end %>
        </tr>
      </tbody>
    </table>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>今週のトレーニング数</th>
          <th>前週のトレーニング数</th>
          <th>前週比</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><%= @this_week_post_record.count %></td>
          <td><%= @last_week_post_record.count %></td>
        <% if @last_week_post_record.count == 0 %>
          <td>前週のトレーニング数が0のため計算できません</td>
        <% else %>
          <td>
            <% @the_week_before = @this_week_post_record.count / @last_week_post_record.count.to_f %>
            <%= (@the_week_before * 100).round %>%
          </td>
        <% end %>
        </tr>
      </tbody>
    </table>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>今月のトレーニング数</th>
          <th>前月のトレーニング数</th>
          <th>前月比</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><%= @this_month_post_record.count %></td>
          <td><%= @last_month_post_record.count %></td>
        <% if @last_month_post_record.count == 0 %>
          <td>前月のトレーニング数が0のため計算できません</td>
        <% else %>
          <td>
            <% @the_month_before = @this_month_post_record.count / @last_month_post_record.count.to_f %>
            <%= (@the_month_before * 100).round %>%
          </td>
        <% end %>
        </tr>
      </tbody>
    </table>
  </div>

if文を使い、条件分岐し前日比 = 昨日の投稿数÷前日の投稿数を定義。
<% @the_week_before = @this_week_post_record.count / @last_week_post_record.count.to_f %>
%= (@the_week_before * 100).round %>%
前週に比べて今週の投稿数が何倍かを計算しています。
.to_fを記述すると小数点まで表示することができる。

参考にさせていただいた記事

https://qiita.com/yuhi_taka/items/675aa9fa404ea32b6a0f
https://pikawaka.com/ruby/to_f
https://pikawaka.com/rails/scope
https://tabakazu.hateblo.jp/entry/2019/12/17/221901

まとめ

一度実装したことがある内容だったけど、scopeメソッドなどうろ覚えな内容が多かったので振り返り大事だなと実感しました。
今日は新しいメカニカルキーボードが届いたので、うきうきでタイピングしています笑
明日も継続頑張ります!

Discussion