Friday, August 22, 2014

How to play with ActiveModel validators in the rails console

1. bundle open activemodel
2. Open the validator.rb file.
3. You can see the comments that shows you how to mixin the validators. Once you mixin, you can add any validator and define a dummy method for the attribute to play with it in the irb:

class Person
  include ActiveModel::Validations
  SSN_REGEX = /your ssn regex goes here/
  validates_format_of :ssn, with: SSN_REGEX
 
 
  def ssn   
    111-11-1111
  end
end

4. Change the ssn method to invalid ssn to test your regex.