Tuesday, February 26, 2013

validation - Validate Datetime in rails - Stack Overflow

validation - Validate Datetime in rails - Stack Overflow


and also:

a.) there's an app for that
gem "validates_date_time", :git => "git://github.com/sofatutor/validates_date_time", :branch => 'rails-3'
b.) I'm actually not using that gem because I found this handy model method before the gem
class KeyPerson < ActiveRecord::Base

validate :started_at_is_date?

private

def started_at_is_date?
   if !started_at.is_a?(Date)
     errors.add(:started_at, 'must be a valid date') 
   end
  end
  end
where :started_at is the column's name. That's a little bit of ruby magic/chaos for you with the dynamic method name handling thing

No comments: