Daniel Morrison

Keep your dates in order with validates_chronological

I think I’ve written code to do this at least 3 times, so today I tried to do it right. Let’s say you have a Rails model with a couple dates (any event with beginning and end will do). You generally want to make sure it ends after it begins.

validates_chronological :begin_time, :end_time

If you try to set end_time before begin_time, you’ll get an error like:

1 errors prohibited this event from being saved

There were problems with the following fields:

  • End time is not in chronological order

It will handle more than 2 fields and is configurable as you’d expect.

Grab the code, which is now part of a plugin we’ll talk about more in the next few weeks.

5 Comments

  1. Tommy_Manson — August 26, 2023

    Wondering if Academized is legit? Yes, it is. Used their service for research paper, got good results. Remember, thorough review helps avoid surprises. https://essayservices.review/reviews/academized-review

  2. strikeduck — September 05, 2023

    Assume you have a Rails model that has a few dates (for every event that has a drift hunters start and finish). In most cases, you want to make sure it concludes once it has started.

  3. Isaac Gimson — September 26, 2023

    I’ve read about video games being studied for stress relief. Can anyone share some scientific findings on this? video games and stress relief studies. https://psndeals.com/blog/impact-of-video-games-on-college-stress-a-comprehensive-analysis/

  4. Isaac Gimson — September 26, 2023

    I’ve read about video games being studied for stress relief. Can anyone share some scientific findings on this? video games and stress relief studies. https://psndeals.com/blog/impact-of-video-games-on-college-stress-a-comprehensive-analysis/

  5. Arise Rentals — September 27, 2023

    Thank you for producing such a fascinating essay on this subject mapquest driving directions. This has sparked a lot of thought in me, and I’m looking forward to reading more.