mongodb/mongoid · error · Mongoid::Errors::NoParent

Cannot persist embedded document %{klass} without a parent d

Error message

Cannot persist embedded document %{klass} without a parent document.

What it means

Error "Cannot persist embedded document %{klass} without a parent document." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/persistable/creatable.rb:57

        { atomic_insert_modifier => { atomic_position => as_attributes } }
      end

      # Insert the embedded document.
      #
      # When the parent association is touchable (which is the default for
      # +embedded_in+), the touch timestamp updates are merged into the
      # same +update_one+ call that performs the insert. This avoids a
      # second round-trip that the +after_save+ touch callback would
      # otherwise issue.
      #
      # @api private
      #
      # @example Insert the document as embedded.
      #   document.insert_as_embedded
      #
      # @return [ Document ] The document.
      def insert_as_embedded
        raise Errors::NoParent.new(self.class.name) unless _parent

        if _parent.new_record?
          _parent.insert
        else
          selector = _parent.atomic_selector
          operations = atomic_inserts

          if _touchable_parent?
            touches = _parent._gather_touch_updates(Time.current)
            if touches.present?
              operations['$set'] = (operations['$set'] || {}).merge(touches)
              Threaded.begin_touch_merged(self)
            end
          end

          _root.collection.find(selector).update_one(
            positionally(selector, operations),
            session: _session

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/persistable/creatable.rb:57 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/e517c6d01138c7c5. Report an issue: GitHub.