Created
March 11, 2025 08:08
-
-
Save giacope/26ce353186dd7d7ffb26a9fca7e81839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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