Skip to content

Instantly share code, notes, and snippets.

@quanon
Created March 4, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quanon/af3d8e569ca753095a5a789f268c1e82 to your computer and use it in GitHub Desktop.
Save quanon/af3d8e569ca753095a5a789f268c1e82 to your computer and use it in GitHub Desktop.
# (あえて境界値がうるう日前後になるように) 今日の日付が 2020/02/29 (土) だと仮定する。
today = Date.parse('2020/02/29')
User.order(:birthday).each { |user| puts("#{user.birthday.strftime('%Y/%m/%d')} #{user.name}")}
# 1989/02/28 ピカチュウ
# 1989/03/01 カイリュー
# 2000/02/29 ヤドラン
# 2000/03/01 ピジョン
age_from = 20 # 20 歳から
age_to = 30 # 30 歳までを検索する。
date_from = today.ago((age_to + 1).years).since(1.day) # ちょうど 31 年前の日付の 1 日後から
date_to = today.ago(age_from.years) # ちょうど 20 年前までを検索する。
User.where(birthday: date_from..date_to).pluck(:name)
#=> ["カイリュー", "ヤドラン"]
@quanon
Copy link
Author

quanon commented Mar 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment