Last active
March 28, 2022 13:20
-
-
Save adrianthedev/4788a38c7e3b27d9a6e0067e057e775f to your computer and use it in GitHub Desktop.
Avo Rails custom validation
This file contains 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 Post < ApplicationRecord | |
validate :url_difference | |
def url_difference | |
# try and find a post with the same URL | |
possible_record = Post.where(url: url).first | |
# If that exists add to errors | |
if possible_record.present? | |
errors.add :url, "A post with that URL exists." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment