为什么 ActiveRecord::Calculations.maximum return TIme 对象而不是 ActiveSupport::TimeWithZone?
Why ActiveRecord::Calculations.maximum return TIme object instead of ActiveSupport::TimeWithZone?
我有这样的代码:
Foo.order(:posted_at).last.posted_at
而且我学会了更好的写法。
Foo.maximum(:posted_at)
但是我注意到 maximum
returns Time
object while another way returns ActiveSupport::TimeWithZone
, 据我所知 Rails 基本上returns TimeWithZone
。为什么maximum
returns正常Time
对象?
Foo.maximum(:posted_at).class
# Time < Object
Foo.order(:posted_at).last.posted_at.class
# ActiveSupport::TimeWithZone < Object
这是 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
maximum
(count
、sum
等)implementation 不与 model
相交并且不进行此类转换。
我有这样的代码:
Foo.order(:posted_at).last.posted_at
而且我学会了更好的写法。
Foo.maximum(:posted_at)
但是我注意到 maximum
returns Time
object while another way returns ActiveSupport::TimeWithZone
, 据我所知 Rails 基本上returns TimeWithZone
。为什么maximum
returns正常Time
对象?
Foo.maximum(:posted_at).class
# Time < Object
Foo.order(:posted_at).last.posted_at.class
# ActiveSupport::TimeWithZone < Object
这是 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
maximum
(count
、sum
等)implementation 不与 model
相交并且不进行此类转换。