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

Invalid dependent strategy

Error message

Invalid dependent strategy: %{invalid_strategy}.

What it means

Depending.validate! is a macro-argument validator: it rejects any :dependent value outside the allowed STRATEGIES enumeration (:delete, :destroy, :nullify, :restrict_with_exception, :restrict_with_error) at class-definition time.

Solutions

  1. Use one of the documented dependent strategies, e.g. dependent: :destroy
  2. Remove the :dependent option if no cascade behavior is wanted
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/association/depending.rb:78 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/758fdd5e676cb124. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/association/depending.rb:78

            klass.dependents_owner = klass
          end

          klass.dependents.push(association) if association.dependent && !klass.dependents.include?(association)
        end
      end

      # Validates that an association's dependent strategy is
      # within the allowed enumeration.
      #
      # @param [ Mongoid::Association::Relatable ] association
      #   The association to validate.
      #
      # @raises [ Mongoid::Errors::InvalidDependentStrategy ]
      #   Error if invalid.
      def self.validate!(association)
        return if STRATEGIES.include?(association.dependent)

        raise Errors::InvalidDependentStrategy.new(association,
                                                   association.dependent,
                                                   STRATEGIES)
      end

      # Perform all cascading deletes, destroys, or nullifies. Will delegate to
      # the appropriate strategy to perform the operation.
      #
      # @example Execute cascades.
      #   document.apply_destroy_dependencies!
      def apply_destroy_dependencies!
        self.class._all_dependents.each do |association|
          if dependent = association.try(:dependent)
            send("_dependent_#{dependent}!", association)
          end
        end
      end

      private

View on GitHub (pinned to 0da0e23d71)