使用 Mongoid 存储时区
我有以下 Mongoid 模型...
class Event
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated
include Mongoid::MultiParameterAttributes
key :name
field :name
field :begins_at, type: Time
field :ends_at, type: Time
belongs_to :city
attr_accessor :time_zone
end
我希望能够有一个每个事件的时区。显然,由于时区的复杂性,处理此问题的最佳方法是在进入数据库时以 UTC 存储时间,并在离开数据库时转换回事件的时区。
我知道 ActiveRecord 对此有支持,我想知道 Mongoid 是否对此有任何类型的本机支持。
I have the following Mongoid model...
class Event
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated
include Mongoid::MultiParameterAttributes
key :name
field :name
field :begins_at, type: Time
field :ends_at, type: Time
belongs_to :city
attr_accessor :time_zone
end
I'd like to be able to have a per-event TimeZone. Obviously due to the complications of time zones the best way to handle this is to store times in UTC when going to the DB and convert back to the event's time zone on the way out of the DB.
I know ActiveRecord has support for this and I was wondering if Mongoid has any kind of native support for this.
存储为:时间戳和查询时格式为时区
Store as: timestamp and when query Format to timezone