Skip to content

Instantly share code, notes, and snippets.

@giacope
Created March 11, 2025 08:08
Show Gist options
  • Select an option

  • Save giacope/26ce353186dd7d7ffb26a9fca7e81839 to your computer and use it in GitHub Desktop.

Select an option

Save giacope/26ce353186dd7d7ffb26a9fca7e81839 to your computer and use it in GitHub Desktop.
class ApplicationRecord < ActiveRecord::Base
around_save :uniq_validation
def uniq_validation
yield
rescue ActiveRecord::RecordNotUnique => e
columns = e.message[/Key \((.+)\)=/, 1]&.split(", ") || [:base]
columns.each { |column| errors.add(column, "must be unique") }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment