mongodb/mongoid · error · ArgumentError

The :fallback option for association '#

Error message

The :fallback option for association '#{name}' on #{@owner_class} cannot be combined with :autobuild.

What it means

Relatable#validate! rejects combining :fallback with :autobuild on the same association. The two options imply conflicting lazy-initialization semantics (proc-supplied default vs auto-built empty document), so both together are disallowed.

Solutions

  1. Keep only one of fallback or autobuild on the association
  2. Implement the default inside the fallback proc if both behaviors were intended
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/association/relatable.rb:433 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/2c96037c423275b1. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/association/relatable.rb:433

        Association::Depending.define_dependency!(self)
      end

      def validate!
        @options.keys.each do |opt|
          unless self.class::VALID_OPTIONS.include?(opt)
            raise Errors::InvalidRelationOption.new(@owner_class, name, opt, self.class::VALID_OPTIONS)
          end
        end

        if @options.key?(:fallback)
          unless @options[:fallback].respond_to?(:call)
            raise ArgumentError,
                  "The :fallback option for association '#{name}' on " \
                  "#{@owner_class} must be a Proc or lambda."
          end

          if @options.key?(:autobuild)
            raise ArgumentError,
                  "The :fallback option for association '#{name}' on " \
                  "#{@owner_class} cannot be combined with :autobuild."
          end
        end

        [ name, :"#{name}?", :"#{name}=" ].each do |n|
          raise Errors::InvalidRelation.new(@owner_class, n) if Mongoid.destructive_fields.include?(n)
        end
      end

      def polymorph!
        return unless polymorphic?

        @owner_class.polymorphic = true
      end

      def create_extension!(&block)
        return unless block

View on GitHub (pinned to 0da0e23d71)