Checking date and time on rails

I am trying to check two date fields. I want to check that the second field always matches the date or any date after the first.

Example if date1 - 05/22/2011 date2 cannot be 05/20/2011.

I am using ruby ​​on rails 3.0.7 and I am trying to do this at the model level.

+6
source share
2 answers

a.) there is an application for this

gem "validates_date_time", :git => "git://github.com/sofatutor/validates_date_time", :branch => 'rails-3' 

b.) I don’t actually use this stone because I found this convenient model method in front of 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 name of the column. This is a bit of ruby ​​magic / chaos for you with a dynamic name processing method.

+3
source

Source: https://habr.com/ru/post/888757/


All Articles