Last active
August 29, 2015 14:06
-
-
Save itsNikolay/8ebefff88d71c8bdf142 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' | |
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