0
0

More than 3 years have passed since last update.

Rails Dateカラムから年単位で値を絞り込み

Posted at

会計情報機能に、2020や2021など、選択した年の収支合計を出すページを作りたく、いろいろと調べていたところ、画期的なGemを見つけました!
Dateモデルの絞り込みができます。

GitHub : by_star

1.Gemをインストール

gemfile
gem 'by_star', git: "git://github.com/radar/by_star"

bundle install

2.使い方
 例1:カラム'created_at'が2020年のPostテーブルの値を取得
 Post.by_year(2020)

例2:Date型カラム'processed_date'(登録日)が2020年のPostテーブルの値を取得
 Post.by_year(2020, field: : processed_date)
 ※field: :カラム名でDate型カラムを指定

支払日や受領日をDate型カラムとして登録し、選択した年分のみの一覧・集計ができるようになりました。
丸二日かけて調べてはDB操作をしてましたが、初心者には少し難しかったです…

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0