LoginSignup
0
0

More than 1 year has passed since last update.

[rails] DateTime#agoがRails5.2まで1.dayといった引数を受け付けていたが6.0からダメになった原因を調べた

Posted at

Rails6.0への更新中、理由が不明だった為調べました

# DateTime#agoの実装
def ago(seconds)
  since(-seconds)
end
# rails 5.2
DateTime.current
=> Fri, 14 May 2021 16:20:00 +0900
DateTime.current.ago(1.day)
=> Thu, 13 May 2021 16:14:26 +0900

# rails 6.0
DateTime.current
=> Fri, 14 May 2021 16:31:41 +0900
DateTime.current.ago(1.day)
TypeError: not an integer
from /bundle/gems/activesupport-6.0.3.7/lib/active_support/core_ext/date_time/calculations.rb:113:in `Rational'

5.2ではagoで呼ばれているDateTime#sinceの実装で、agoの引数をroundしてintegerとなっていたのが、roundしなくなりRationalの引数として不適切になったから出たエラーのようです。
PRを見た感じ、昔はroundをする意味があったが今は無いのでしなくなったという事かな?
そもそも、secondsを要求されているので1.dayといった値を渡していたのがおかしくて、たまたまroundされていただけで意図しない使い方していた気がする...!

素直にこう書いておきます

DateTime.current - 1.day 
=> Thu, 13 May 2021 16:34:06 +0900
0
0
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
0
0