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

Always touch parents of embedded documents when embedded documents are touched

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.3.0
    • Affects Version/s: None
    • Component/s: Associations, Persistence
    • None
    • Minor Change

      As discussed in MONGOID-1552 and MONGOID-2852 (and possibly others), currently Mongoid won't update timestamps of parent documents if embedded documents are changed. I believe that if this is not a bug, at the very least it violates the principle of least surprise, because embedded documents are, by definition, part of the document that contains them. Updates to an embedded document are literally updates to the root document.

      The current functionality presents a challenge for applications that use updated_at for cache expiry. This has presented logical issues for me in 3 separate Rails apps, and I had to hack around the real problem each time. The only place I could make a change, and not feel dirty, is in Mongoid itself.

      This pull request also exposes and corrects (5cf9be1) a bug in touch which prior to this pull request would cause modified documents to be saved in their entirety when touch promises only to affect the updated_at field. The problem came down to the regular expression used to touch documents; when the optional field was nil, the regular expression became /updated_at|/, which is guaranteed to match all attribute names. Thus, touch would save all modified attributes and not just the updated_at field.

      This PR ensures that parent documents get their timestamps updated whenever an embedded document is created, updated, deleted or touched. It also affects parents of parents, all the way up to the root document. The changes I made did not affect other tests, and they are all passing for me. Further, using this PR in my most recent Rails app has solved all of the issues I've been fighting with for several days.

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

              Created:
              Updated:
              Resolved: