Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4770

Mongoid does not automatically require driver ActiveSupport compatibility

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.0.rc0, 7.0.6
    • Affects Version/s: 7.0.5
    • Component/s: None
    • None
    • Environment:
      OS X, rails 5.2.3

      Per the documentation in https://docs.mongodb.com/ruby-driver/master/reference/driver-compatibility/#rails-activesupport-compatibility, Mongoid as of 7.0.3 is supposed to automatically require mongo/active_support, but I am unable to locate the code which would do this. Lacking this require, Mongoid currently incorrectly serializes AS::TimeWithZone instances.

      Original report:

      ----------------------------------

      I'm having issues querying using a ActiveSupport::TimeWithZone value - it seems to be ignoring the timezone entirely

      I can reproduce with this setup: 

      a new rails 5.2.3 app with mongoid 5.2.3 and this mongoid.yml file

      development:
        clients:
          default:
            database: "dummy-development"
            hosts:
            - "localhost"
        options:
          use_utc: true
          use_activesupport_time_zone: true

      and this very simple model / test data:

      class Dummy
        include Mongoid::Document
        field :time
      end
      
      Dummy.create(time: Time.mktime(2019,9,1).utc)
      

      Then this query returns the newly created object, as expected:

      {{ Dummy.where(time:

      {'$gte': Time.mktime(2019,9,1).utc}

      ).to_a }}

      However this query returns no results

      {{ Dummy.where(time:

      {'$gte': Time.mktime(2019,9,1).in_time_zone("Europe/Paris")}

      ).to_a }}
       
      I was expecting this to return the same results as both of these 2 values represent the same instant in time, this is also the behaviour that active record exhibits.

      If I require 'bson/time_with_zone' then I start getting the results i expected.

      It seems that without this the call to to_bson on the ActiveSupport::TimeWithZone get handled by the method_missing implementation provided by activesupport ( https://github.com/rails/rails/blob/d0e813fc1225278df5c12e7a0a4a0c65d0c0e1ac/activesupport/lib/active_support/time_with_zone.rb#L521 ) which works by stripping the time zone, calling the method on the underlying Time object and then trying to add the timezone back, which of course only works on methods that turn an instance of Time into another one.

      I tried changing the options in my mongoid.yml file, but this didn't seem to have any effect.

      Is this the expected behaviour ? Is requiring bson/time_with_zone a suitable workaround?

      Thanks

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            fcheung Frederick Cheung
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: