Skip to content

Instantly share code, notes, and snippets.

@itsNikolay
Last active August 29, 2015 14:06
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/8ebefff88d71c8bdf142 to your computer and use it in GitHub Desktop.
Save itsNikolay/8ebefff88d71c8bdf142 to your computer and use it in GitHub Desktop.
require 'active_record'
require "active_support"
class Person < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: 'foobar.db'
connection.create_table table_name, force: true do |t|
t.date :date
end
validate :is_date?
def is_date?
errors.add(:date, "must be a valid date") unless date.try(:to_date)
end
end
p1 = Person.new(date: Date.current)
p p1.errors unless p p1.valid?
p1= Person.new(date: 'wrong')
p p1.errors unless p p1.valid?
#> ruby test.rb
#true
#false
#<ActiveModel::Errors:0xb9486598 @base=#<Person id: nil, date: nil>, @messages={:date=>["must be a valid date"]}>
#<ActiveRecord::Relation []>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment