Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Created February 3, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/030654294e5a129e4ab51a9bcf22669c to your computer and use it in GitHub Desktop.
Save PerezIgnacio/030654294e5a129e4ab51a9bcf22669c to your computer and use it in GitHub Desktop.
Custom validator example
class MyValidator < ActiveModel::Validator
def validate(record)
return if record.name.starts_with? 'X'
record.errors[:name] << 'Need a name starting with X please!'
end
end
class Person < ApplicationRecord
validates_with MyValidator
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment