{"record":{"id":"9b16d7ce1f334c76","repo":"thoughtbot/shoulda-matchers","slug":"validate-uniqueness-of-works-by-matching-a-new-rec","errorCode":null,"errorMessage":"validate_uniqueness_of works by matching a new record against an\nexisting record. If there is no existing record, it will create one\nusing the record you provide.\n\nWhile doing this, the following error was raised:\n\n#{Shoulda::Matchers::Util.indent(underlying_exception.message, 2)}\n\nThe best way to fix this is to provide the matcher with a record where\nany required attributes are filled in with valid values beforehand.","messagePattern":"validate_uniqueness_of works by matching a new record against an\nexisting record\\. If there is no existing record, it will create one\nusing the record you provide\\.\n\nWhile doing this, the following error was raised:\n\n#(.+?)\n\nThe best way to fix this is to provide the matcher with a record where\nany required attributes are filled in with valid values beforehand\\.","errorType":"exception","errorClass":"ExistingRecordInvalid","httpStatus":null,"severity":"error","filePath":"lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb","lineNumber":599,"sourceCode":"        def find_or_create_existing_record\n          @existing_record = find_existing_record\n\n          unless @existing_record\n            @existing_record = create_existing_record\n            @existing_record_created = true\n          end\n        end\n\n        def find_existing_record\n          model.first.presence\n        end\n\n        def create_existing_record\n          @given_record.tap do |existing_record|\n            existing_record.save(validate: false)\n          end\n        rescue ::ActiveRecord::StatementInvalid => e\n          raise ExistingRecordInvalid.create(underlying_exception: e)\n        end\n\n        def update_existing_record!(value)\n          if existing_value_read != value\n            set_attribute_on_existing_record!(@attribute, value)\n            # It would be nice if we could ensure that the record was valid,\n            # but that would break users' existing tests\n            existing_record.save(validate: false)\n          end\n\n          true\n        end\n\n        def arbitrary_non_blank_value\n          non_blank_value = dummy_value_for(@attribute)\n          limit = column_limit_for(@attribute)\n\n          is_string_value = non_blank_value.is_a?(String)","sourceCodeStart":581,"sourceCodeEnd":617,"githubUrl":"https://github.com/thoughtbot/shoulda-matchers/blob/79cdfbc326e97c9b43991b644434ca53c1f62c4c/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb#L581-L617","documentation":"Error \"validate_uniqueness_of works by matching a new record against an\nexisting record. If there is no existing record, it will create one\nusing the record you provide.\n\nWhile doing this, the following error was raised:\n\n#{Shoulda::Matchers::Util.indent(underlying_exception.message, 2)}\n\nThe best way to fix this is to provide the matcher with a record where\nany required attributes are filled in with valid values beforehand.\" thrown in thoughtbot/shoulda-matchers.","triggerScenarios":"Thrown at lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb:599 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":"79cdfbc326e97c9b43991b644434ca53c1f62c4c","analyzedAt":"2026-08-23T09:43:24.200Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}