Ather Shiraz
2008-May-09 14:21 UTC
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2008-May-09 16:19 UTC
Re: How to Validate Uniqueness Of a Record within Date Range
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---