mongodb/mongoid · error · ArgumentError

Eager loading only supports arguments that are the names of…

Error message

Eager loading only supports arguments that are the names of associations on #{klass}

What it means

Eager-load argument validation: includes/eager_load only accepts names of associations defined on the criteria's class (or its descendants). A name matching no relation — a typo, or one defined only on an unrelated class — raises this ArgumentError when eager loading is attempted.

Solutions

  1. Pass association names that exist on the model (check klass.relations.keys)
  2. Fix the spelling of the association name in includes/eager_load
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

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

      # @param [ Class ] parent_class The class to start the lookup from.
      # @param [ Symbol | String ] relation The association name.
      # @param [ Boolean ] is_eager_load Whether to consider subclasses.
      #
      # @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)