[Ruby] [Validation] rails How to specify after today’s date

less than 1 minute read


When entering a date, you can only enter dates after today


■ Improvement of UX
■ Improved understanding of validate

【Development environment】

■ Mac OS catalina
■ Ruby on Rails (
■ Virtual Box:6.1
■ Vagrant: 2.2.7


  • Create validate with any method name
  • Added method processing
    ① Judge whether it is empty by “return if start_day.blank?”
    ② Get today’s date with “if start_day <Date.today” & conditional branch & comparison
    ③ Define the error statement in “errors.add”


validate :date_before_start
validate :date_before_finish

  def date_before_start
    return if start_day.blank?
    errors.add(:start_day, "Please select the one after today") if start_day < Date.today

  def date_before_finish
    return if finish_day.blank? || start_day.blank?
    errors.add(:finish_day, "Please select the one after the start date") if finish_day < start_day

that’s all! !!

[I want to read it together]

■ Differences between Time, Date, DateTime, TimeWithZone between Ruby and Rails

■ [ERROR message display] A simplified version that can be used at any time with the rails partial template.

■ [Date.today] How to retrieve information only for today rails