Skip to content

Instantly share code, notes, and snippets.

@ankit8898
Created September 4, 2012 13:25
Show Gist options
  • Save ankit8898/3621130 to your computer and use it in GitHub Desktop.
Save ankit8898/3621130 to your computer and use it in GitHub Desktop.
Custom date validation to prevent the save of default date in Rails Activerecord with validates_with
Place this file in your config/initializers
class CheckDate < ActiveModel::Validator
#Sat Jan 01 00:00:00 UTC 2000 is the default date that rails store
def validate(record)
if record.start_time == "Sat Jan 01 00:00:00 UTC 2000".to_date
record.errors[:base] << "This is a invalid date"
end
end
end
Schema entry
#t.time "start_time", :null => false
#t.datetime "created_at"
#t.datetime "updated_at"
class MyModel < ActiveRecord::Base
validates_with CheckDate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment