32
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails(ActiveRecode)で一週間分のレコードを取得する

Last updated at Posted at 2015-06-05

取得方法

ActiveSupport の日付関連の拡張を使用する。

item.rb
from  = Time.current.at_beginning_of_day
to    = (from + 6.day).at_end_of_day
items = Item.where(created_at: from...to)

その他

一月間のデータ取得
from  = Time.current.at_beginning_of_day
to    = (from + 1.month)
items = Item.where(created_at: from...to)
一年間のデータ取得
to    = Time.current.at_beginning_of_day
from  = (to - 1.year)
items = Item.where(created_at: from...to)

参考サイト

ActiveSupport の日付関連の拡張のドキュメント

Dateクラス
http://railsdoc.com/references/beginning_of_day#日付関連

DateTimeクラス
http://railsdoc.com/references/beginning_of_day#日時関連

Timeクラス
http://railsdoc.com/references/beginning_of_day#時間関連(TIme)

32
35
1

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
32
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?