{"record":{"id":"bc4add5d2f62dad1","repo":"mongodb/mongoid","slug":"attempted-to-access-attribute-name-on-klass","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/association/accessors.rb","lineNumber":120,"sourceCode":"      # @param [ true | false ] reload If the association is to be reloaded.\n      #\n      # @return [ Proxy ] The association.\n      def get_relation(name, association, object, reload = false)\n        field_name = database_field_name(name)\n\n        # As per the comments under MONGOID-5034, I've decided to only raise on\n        # embedded associations for a missing attribute. Rails does not raise\n        # for a missing attribute on referenced associations.\n        # We also don't want to raise if we're retrieving an association within\n        # the codebase. This is often done when retrieving the inverse association\n        # during binding or when cascading callbacks. Whenever we retrieve\n        # associations within the codebase, we use without_autobuild.\n        if !without_autobuild? && association.embedded? && attribute_missing?(field_name)\n          # We always allow accessing the parent document of an embedded one.\n          try_get_parent = association.is_a?(\n            Mongoid::Association::Embedded::EmbeddedIn\n          ) && field_name == association.key\n          raise Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) unless try_get_parent\n        end\n\n        if !reload && (value = ivar(name)) != false\n          value\n        else\n          _building do\n            _loading do\n              if object && needs_no_database_query?(object, association)\n                __build__(name, object, association)\n              # Check if data was loaded via $lookup aggregation\n              elsif !association.embedded? && attributes.key?(name.to_s)\n                # Use the pre-loaded association data from $lookup\n                __build__(name, attributes[name.to_s], association)\n              else\n                selected_fields = _mongoid_filter_selected_fields(association.key)\n                __build__(name, attributes[association.key], association, selected_fields)\n              end\n            end","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/association/accessors.rb#L102-L138","documentation":"Error \"Attempted to access attribute '%{name}' on %{klass} which was not loaded.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/association/accessors.rb:120 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"}