LoginSignup
8
8

More than 5 years have passed since last update.

Railsで月単位で値を抽出する

Last updated at Posted at 2014-11-20

Railsで月単位で値を抽出したい時は、 ActiveSupportのTime拡張を使えば簡単にできますby_starを使いましょう!

ActiveSupportのTime拡張でやった場合、MySQLの「BETWEEN2014/11/1 AND 2014/11/21」なら 21未満の値をとります。よって、以下コード最終日が取れないので間違っていました!

こんな感じでやると、年と月指定ができました\(^o^)/

Model.by_year(Time.zone.year, field: :created_at)by_month(Time.zone.month, field: :created_at)

by_starすごい!簡単!便利!

8
8
2

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
8
8