LoginSignup
0
0

More than 5 years have passed since last update.

特定のDateから指定した日付分のRangeを返させる

Posted at

Railsのクエリ検索で3ヶ月以内に更新されたものを調べる時に

Post.where(updated_at: (date - 3.months)..date))

のようなことをよくするので、Date#rangeという Rangeクラスを返すメソッドを定義してみた。

class Date
  def range(duration)
    Range.new([(self + duration),self].min, [self + duration, self].max)
  end
end

これでさっきの検索が

Post.where(updated_at: date.range(-3.months))

にできます。

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