Created
October 7, 2014 14:57
-
-
Save itsNikolay/7bc0b946770da4bf039a 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
require 'active_record' | |
class Person < ActiveRecord::Base | |
establish_connection adapter: 'sqlite3', database: 'foobar.db' | |
connection.create_table table_name, force: true do |t| | |
t.string :mobile_no | |
end | |
validate do | |
regexp = /^(07[\d]{9})/ | |
unless mobile_no.match(regexp) | |
errors.add(:base, 'has wrong format') | |
end | |
end | |
end | |
bob = Person.new(mobile_no: '07000000000') | |
p bob.valid? | |
p bob.errors.to_a | |
john = Person.new(mobile_no: 'wrong') | |
p john.valid? | |
p john.errors.to_a | |
# > ruby test16.rb | |
# true | |
# [] | |
# false | |
# ["has wrong format"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment