LoginSignup
17
17

More than 5 years have passed since last update.

ActiveSupportで時間や日付を簡単に表現する

Last updated at Posted at 2012-09-12

active_supportには時間に関する拡張がたくさんあり、非常に便利になっています。
たとえば、ありがちな今月の投稿を取得するような場合はこんな感じで超シンプルにかけます

Post.where(created_at: Time.now.all_month) #all_monthはレンジオブジェクトを返す

他にも1日を表すall_dayや1年を表すall_year月末を表すat_end_of_monthや翌月を表すnext_monthなど、自分で実装するとテストを書かなければならないような便利拡張がいっぱいです。

下記URLでTimeのメソッドがみれますので、一読しておいて損はないでしょう
http://api.rubyonrails.org/classes/Time.html

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