{"record":{"id":"c6a8f1727ef1c5ed","repo":"mongodb/mongoid","slug":"attempted-to-access-attribute-name-on-klass-c6a8f1","errorCode":null,"errorMessage":"Attempted to access attribute '%{name}' on %{klass} which was not loaded.","messagePattern":"Attempted to access attribute '%(.+?)' on %(.+?) which was not loaded\\.","errorType":"exception","errorClass":"Mongoid::Errors::AttributeNotLoaded","httpStatus":null,"severity":"error","filePath":"lib/mongoid/attributes.rb","lineNumber":167,"sourceCode":"\n    # Write a single attribute to the document attribute hash. This will\n    # also fire the before and after update callbacks, and perform any\n    # necessary typecasting.\n    #\n    # @example Write the attribute.\n    #   person.write_attribute(:title, \"Mr.\")\n    #\n    # @example Write the attribute (alternate syntax.)\n    #   person[:title] = \"Mr.\"\n    #\n    # @param [ String | Symbol ] name The name of the attribute to update.\n    # @param [ Object ] value The value to set for the attribute.\n    def write_attribute(name, value)\n      validate_writable_field_name!(name.to_s)\n\n      field_name = database_field_name(name)\n\n      raise Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) if attribute_missing?(field_name)\n\n      if attribute_writable?(field_name)\n        _assigning do\n          localized = fields[field_name].try(:localized?)\n          attributes_before_type_cast[name.to_s] = value\n          typed_value = typed_value_for(field_name, value)\n          unless attribute_will_not_change?(field_name, typed_value) || attribute_changed?(field_name)\n            attribute_will_change!(field_name)\n          end\n          if localized\n            present = fields[field_name].try(:localize_present?)\n            loc_key, loc_val = typed_value.first\n            if present && loc_val.blank?\n              attributes[field_name]&.delete(loc_key)\n            else\n              attributes[field_name] ||= {}\n              attributes[field_name].merge!(typed_value)\n            end","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/attributes.rb#L149-L185","documentation":"Error \"Attempted to access attribute '%{name}' on %{klass} which was not loaded.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/attributes.rb:167 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}