# Validate unique within date range?

**URL:** <https://www.ruby-forum.com/t/validate-unique-within-date-range/93230>\
**Category:** Rails\
**Created:** [April 29, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/validate-unique-within-date-range/93230 "2007-04-29T18:41:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 29, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/validate-unique-within-date-range/93230/1 "2007-04-29T18:41:10Z")

</div>

I’m setting up a simple system for listing products and specials. I’m  
looking for a way to validate that only one special can exist per  
product per date range. For example, if a product is on sale from  
2007-04-29 to 2007-05-10, you cannot create a special for that product  
that starts on 2007-05-09.

What’s the best way to do this with Rails? I’m fairly new to RoR. I’ve  
been looking for a way to do this with model validators, because I want  
it to work as smoothly as the regular presence validators and all that.  
Any help would be appreciated. I expect I’m going to have to write a  
custom function to check this (the query is easy enough), but I’m not  
sure where to put it or how to integrate it with your standard  
Special.new call and all that.

The models in question:

Product  
:id  
:name  
:description  
:category  
:price

Special  
:id  
:product\_id  
:start\_on (date)  
:end\_on (date)

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [May 1, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/validate-unique-within-date-range/93230/2 "2007-05-01T12:42:52Z")

</div>

Matthew R. wrote:

> I’m setting up a simple system for listing products and specials. I’m  
> looking for a way to validate that only one special can exist per  
> product per date range. For example, if a product is on sale from  
> 2007-04-29 to 2007-05-10, you cannot create a special for that product  
> that starts on 2007-05-09.

use validate uniques for that column?

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [May 3, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/validate-unique-within-date-range/93230/3 "2007-05-03T16:11:13Z")

</div>

Jamal S. wrote:

> Matthew R. wrote:
> 
> > I’m setting up a simple system for listing products and specials. I’m  
> > looking for a way to validate that only one special can exist per  
> > product per date range. For example, if a product is on sale from  
> > 2007-04-29 to 2007-05-10, you cannot create a special for that product  
> > that starts on 2007-05-09.
> 
> use validate uniques for that column?

How would validate uniques do me any good? I’m trying to make sure a new  
start date is not in the same _range_ as another entry with the same  
product\_id. I wound up just running my own check manually before I  
create the new Special, but it’s ugly and I’m still wondering if there’s  
a more elegant way to integrate it into the Special class so it’s  
automatically checked whenever I call ‘new’.
