0
我有這樣的代碼:爲什麼ActiveRecord :: Calculations.maximum返回TIme對象而不是ActiveSupport :: TimeWithZone?
Foo.order(:posted_at).last.posted_at
而且我知道更好的方式來寫。
Foo.maximum(:posted_at)
但是我注意到maximum
回報Time
對象,而另一種方式返回ActiveSupport::TimeWithZone
,而據我所知Rails的基本返回TimeWithZone
。爲什麼maximum
返回正常Time
對象?
Foo.maximum(:posted_at).class
# Time < Object
Foo.order(:posted_at).last.posted_at.class
# ActiveSupport::TimeWithZone < Object
這是'activerecord'問題:模型實例化和計算(最大值,總和等)以不同的方式。模型實例轉換爲'ActiveSupport :: TimeWithZone',而計算代碼不與模型代碼相交,並且不會投射 –
我明白了,謝謝!如果您發表評論作爲答案,我會接受它。 – ironsand