Rectangle 27 0

mongodb How to save a timezone correctly with Ruby and MongoId?


class Lineup
  include Mongoid::Document
  field :date, type: Time
end
defaults: &defaults
  use_utc: false
  use_activesupport_time_zone: true

Finally, have a look at the mongoid-metastamp gem. It will give you much better support for querying across multiple timezones, while still seamlessly working like a native Time field.

It looks like you need to specify the field type of your date attribute. I would use a Time field if you want mongoid to handle the zones properly.

This sounds counterintuitive, but this is the current way to make mongoid use UTC as the default timezone.

This works with DateTime too in the current versions of Mongoid (I'm on 2.4.7). See github.com/mongoid/mongoid/issues/1135

You will also probably want to set the following in config/mongoid.yml

Note