Validate unique within date range?


#1

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)


#2

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?


#3

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’.