Skip to content

Instantly share code, notes, and snippets.

@adrianthedev
Last active March 28, 2022 13:20
Show Gist options
  • Save adrianthedev/4788a38c7e3b27d9a6e0067e057e775f to your computer and use it in GitHub Desktop.
Save adrianthedev/4788a38c7e3b27d9a6e0067e057e775f to your computer and use it in GitHub Desktop.
Avo Rails custom validation
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