2017-07-14 112 views
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 
+1

這是'activerecord'問題:模型實例化和計算(最大值,總和等)以不同的方式。模型實例轉換爲'ActiveSupport :: TimeWithZone',而計算代碼不與模型代碼相交,並且不會投射 –

+0

我明白了,謝謝!如果您發表評論作爲答案,我會接受它。 – ironsand

回答

0

這是Activerecord問題。鑄造到ActiveSupport::TimeWithZone實施model級別。嘗試在控制檯:

model_instance = MyModel.new 
t = Time.now # not Time.current 
model_instance.created_at.class 
t.class # Time 
model_instance.created_at = t 
model_instance.created_at.class # ActiveSupport::TimeWithZone 

maximumcountsum等)implementationmodel一個相交,並沒有做這樣的鑄造。

相關問題