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

correctly persist Date and DateTime fields to mongoid

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      attached are the specs and code to use serialized values for changes that are being persisted. this will correctly persist Date and DateTime fields to mongodb, during an update of the document.

      the use of the base ActiveModel::Dirty#attribute_change method to read changed values was calling the attribute's get accessor, causing a deserialization of the value (via the serialization/date and serialization/date_time modules) for Date and DateTime values. This was causing Mongoid to throw an exception saying the document was invalid due to Date or DateTime values.

      the fix i put in place is to take the underlying code from ActiveModel::Dirty#attribute_changed, and instead of calling _send_(atr), call attributes[attr] to get the already serialized value from the model's attributes. I pulled the code for ActiveModel::Dirty#attribute_changed from http://apidock.com/rails/ActiveModel/Dirty/attribute_change and modified it accordingly, in the Dirty module of Mongoid.

      i'm not certain that this is the best solution. however, it's functional and all specs pass. if there's a better solution, i'm all for it... i just need a solution in place so that my apps can use Date / DateTime fields correctly.

            Assignee:
            Unassigned Unassigned
            Reporter:
            derickbailey derickbailey
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: