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

Attempted to access attribute '%{name}' on %{klass} which wa

Error message

Attempted to access attribute '%{name}' on %{klass} which was not loaded.

What it means

Error "Attempted to access attribute '%{name}' on %{klass} which was not loaded." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/attributes.rb:167

    # Write a single attribute to the document attribute hash. This will
    # also fire the before and after update callbacks, and perform any
    # necessary typecasting.
    #
    # @example Write the attribute.
    #   person.write_attribute(:title, "Mr.")
    #
    # @example Write the attribute (alternate syntax.)
    #   person[:title] = "Mr."
    #
    # @param [ String | Symbol ] name The name of the attribute to update.
    # @param [ Object ] value The value to set for the attribute.
    def write_attribute(name, value)
      validate_writable_field_name!(name.to_s)

      field_name = database_field_name(name)

      raise Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) if attribute_missing?(field_name)

      if attribute_writable?(field_name)
        _assigning do
          localized = fields[field_name].try(:localized?)
          attributes_before_type_cast[name.to_s] = value
          typed_value = typed_value_for(field_name, value)
          unless attribute_will_not_change?(field_name, typed_value) || attribute_changed?(field_name)
            attribute_will_change!(field_name)
          end
          if localized
            present = fields[field_name].try(:localize_present?)
            loc_key, loc_val = typed_value.first
            if present && loc_val.blank?
              attributes[field_name]&.delete(loc_key)
            else
              attributes[field_name] ||= {}
              attributes[field_name].merge!(typed_value)
            end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/attributes.rb:167 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/c6a8f1727ef1c5ed. Report an issue: GitHub.