Created
March 4, 2019 14:01
-
-
Save quanon/af3d8e569ca753095a5a789f268c1e82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (あえて境界値がうるう日前後になるように) 今日の日付が 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) | |
#=> ["カイリュー", "ヤドラン"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://qiita.com/Yusya/items/aad599c2194aa3d4dacc を読んで考えてみた。