# Validating that end\_date doesn't come before start\_date

**URL:** <https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048>\
**Category:** Rails\
**Created:** [September 20, 2007, 5:39am UTC](https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048 "2007-09-20T05:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@knb](https://www.ruby-forum.com/u/knb)\
**Post date:** [September 20, 2007, 5:39am UTC](https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048/1 "2007-09-20T05:39:05Z")

</div>

A user enters two dates. On before\_save, I’m trying to validate that  
the second date does _not_ come before the first.

Assume that start\_date and end\_date are Datetime objects. If I call  
the following method from the model on before\_save, why is no error  
added to end\_date?

def assure\_dates\_in\_order  
start = Time.at(self.start\_date)  
last = Time.at(self.end\_date)  
if (start - last) \> 0  
errors.add(:base, “can’t come before start date”)  
end  
end

I know know that this code is quite ugly. I could not get the  
Datetime objects to compare any other way.

Anyone who can make this code beautiful and functional is awesome.

---

<div class="post-metadata">

**Author:** ![knb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@knb](https://www.ruby-forum.com/u/knb)\
**Post date:** [September 20, 2007, 6:07am UTC](https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048/2 "2007-09-20T06:07:07Z")

</div>

If both are dates, this will work:

def assure\_dates\_in\_order  
errors.add\_to\_base(“can’t come before start date”) unless  
self.start\_date \< self.end\_date  
end

knb wrote:

> if (start - last) \> 0
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![knb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@knb](https://www.ruby-forum.com/u/knb)\
**Post date:** [September 20, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048/3 "2007-09-20T16:15:35Z")

</div>

btw, after looking at the way your error message reads, it looks like  
you want this to appear on the “end\_date” field. This would do that:

def assure\_dates\_in\_order  
errors.add(:end\_date,“can’t come before start date”) unless  
self.start\_date \< self.end\_date  
end

William P. wrote:

> > A user enters two dates. On before\_save, I’m trying to validate that  
> > errors.add(:base, “can’t come before start date”)

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![knb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@knb](https://www.ruby-forum.com/u/knb)\
**Post date:** [September 20, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/validating-that-end-date-doesnt-come-before-start-date/115048/4 "2007-09-20T16:23:38Z")

</div>

[http://svn.viney.net.nz/things/rails/plugins/validates\_date\_time/](http://svn.viney.net.nz/things/rails/plugins/validates_date_time/)
