Fix #4999: Unexpected error: ActiveRecord::RecordNotUnique sometimes appears when uploading posts
Fix two issues that could lead to duplicate errors when creating posts: * Fix the submit button on the upload form to disable itself on submit, to prevent accidental double submit errors. * Fix a race condition when checking for MD5 duplicates. MD5 uniqueness is checked on both the Rails level, with a uniqueness validation, and on the database level, with a unique index on the md5 column. Creating a post could fail with an ActiveRecord::RecordNotUnique error if the uniqueness validation in Rails passed, but the uniqueness constraint in the database failed. In this case, we catch the RecordNotUnique error and convert it to a Rails validation error so we can treat it like a normal validation failure.
This commit is contained in:
@@ -172,6 +172,16 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Save the record, but convert RecordNotUnique exceptions thrown by the database into
|
||||
# Rails validation errors. This way duplicate records only return one type of error.
|
||||
# This assumes the table only has one uniqueness constraint in the database.
|
||||
def save_if_unique(column)
|
||||
save
|
||||
rescue ActiveRecord::RecordNotUnique => e
|
||||
self.errors.add(column, :taken)
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
concerning :UserMethods do
|
||||
|
||||
Reference in New Issue
Block a user