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

Errors::InvalidIncludes.new(klass, [ relation ])

Error message

Errors::InvalidIncludes.new(klass, [ relation ])

What it means

Includes validation for the non-eager path: the relation name given to includes does not resolve to an association on the class, so the inclusion cannot be set up and InvalidIncludes is raised naming the offending relation.

Solutions

  1. Use a declared association name in includes
  2. Inspect Model.relations to find the correct association names
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/criteria/includable.rb:144 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/e10fe2b2ce46bc54. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/criteria/includable.rb:144

      #
      # @return [ Array<Mongoid::Association::Relatable> ] Matching associations.
      def resolve_inclusion_associations(parent_class, relation, is_eager_load)
        if association = parent_class.reflect_on_association(relation)
          return [ association ]
        end

        return [] unless is_eager_load

        parent_class.descendants.filter_map { |sub| sub.reflect_on_association(relation) }
      end
    end

    def raise_eager_error(is_eager_load, klass, relation)
      if is_eager_load
        raise ArgumentError, "Eager loading only supports arguments that are the names of associations on #{klass}"
      end

      raise Errors::InvalidIncludes.new(klass, [ relation ])
    end
  end
end

View on GitHub (pinned to 0da0e23d71)