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

Query by Time field regression

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • None

      I'm using a Time field to keep track of our accounts periods of consumption (monthly storage/transfer quotas). After upgrading Mongoid 4 from sha cb541fa1fd7cf9ab0a725c757490d0ac435a79f7 to 10d351f3e1e75c701e0fbc4be0c22c4284fff9b1 I noticed that our specs started randomly failing.

      Boiling it down a bit it looks like the Time value that I set the field to is not consistent with what actually ends up in the database.

      Reproduce with:

      class Account
        include Mongoid::Document
      
        field :period_started_at, type: Time
      
        has_many :consumption_periods, dependent: :destroy, validate: false
      
        def current_consumption
          consumption_periods.find_or_create_by(started_at: period_started_at)
        end
      end
      
      class ConsumptionPeriod
        include Mongoid::Document
      
        belongs_to :account
      
        field :started_at, type: Time
      end
      
      Account.destroy_all
      
      100.times do |i|
        print "."
      
        account = Account.create!(period_started_at: Time.now.utc)
        created_consumption = account.current_consumption
      
        account.reload # changes the value of period_started_at, sometimes causing the consumption reference to break
      
        unless created_consumption == account.current_consumption
          puts "Error: expected #{created_consumption} but was #{account.current_consumption}"
        end
      end
      

      I've noticed other issues on github relating to Time being slightly different in Ruby-land vs on the Database but as this has worked for us for years using Mongoid I expect it can be fixed.

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            dbackeus David Backeus
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: