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

How to save a timezone correctly with Ruby and MongoId?


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.

class Lineup  include Mongoid::Document  field :date, type: Timeend

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

defaults: &defaults  use_utc: false  use_activesupport_time_zone: true

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

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.