How to Validate Uniqueness Of a Record within Date Range

I would like to validate the uniqueness of a record within a date range.

For example : I want a customer attached to a post office within a date
range arrives(date) and departs (date).

So this is ok:
customer1; postoffice1; Feb1(arrives);Mar1(departs)
customer1; postoffice1; Mar2(arrives);Apr1(departs)

This however is NOT ok:
customer1; postoffice1; Feb1(arrives);Mar1(departs)
customer1; postoffice1; Jan1(arrives);Feb10(departs) [xxx overlapping
dates xxx]

The validation clause for this would be?

? validates_uniqueness_of :customer_id, :scope =>
[:postoffice_id,:arrives,:departs], :message=>“This unit with this
arrival date for this PSO already exists.”
?

I dont think the above is right? How do I specify range within scope.

I don’t see a way for you to combine the high-level validations to do
what
you want. However, you can overwrite the validate method to do what you
want.

class SomeModel < ActiveRecord::Base
def validate
#custom validation here
end
end

See
http://www.railsbrain.com/api/edge/doc/index.html?a=C00000457&name=Validations,
which shows an example Person model that uses the high-level
validations
and overwrites validate. It also shows how to add errors if your custom
validation fails.

Regards,
Craig

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs