Forum: Ruby on Rails Validate unique within date range?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B1c20262609ca49fe15a157c6f935035?d=identicon&s=25 Matthew Rogers (mrogers)
on 2007-04-29 18:41
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)
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-05-01 12:42
Matthew Rogers 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?
B1c20262609ca49fe15a157c6f935035?d=identicon&s=25 Matthew Rogers (mrogers)
on 2007-05-03 16:11
Jamal Soueidan wrote:
> Matthew Rogers 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'.
This topic is locked and can not be replied to.