Skip to content

Instantly share code, notes, and snippets.

@itsNikolay
Created October 7, 2014 14:57
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 itsNikolay/7bc0b946770da4bf039a to your computer and use it in GitHub Desktop.
Save itsNikolay/7bc0b946770da4bf039a to your computer and use it in GitHub Desktop.
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