On Wed, Feb 29, 2012 at 3:53 PM, LED <[email protected]> wrote:

> hi im new in rails and starting to develop my application which is a
> online reservation im just wondering if there is a way in Ruby on
> rails to group a validation
>
> ei. i want to validate the uniqueness of date first in the database
> and check what are the timeStart and endTime entries in that
> particular date and validate its uniqueness(timeStart and endTime)
> base on the date? is it possible in validation or do i have to make a
> controller or method for it?
>
> to sum up my application can book a reservation in a same date but not
> in a same date and time
>
> sorry for my bad english my Reservation database is:
> 0           id          INTEGER     1                       1
> 1           lname       varchar(25  1                       0
> 2           fname       varchar(25  1                       0
> 3           contactnum  integer     1                       0
> 4           email       varchar(10  1                       0
> 5           date        date        1           '2012-03-0  0
> 6           timeStart   time        1           '---
> :hour  0
> 7           timeEnd     time        1           '---
> :hour  0
> 8           numGuest    integer     0           5           0
> 9           totalCost   decimal(2)  0                       0
> 10          confirm     boolean     0           'f'         0
> 11          created_at  datetime    0                       0
> 12          updated_at  datetime    0                       0
>


1) Check the rails guide on validations

http://guides.rubyonrails.org/active_record_validations_callbacks.html

section 3.10

The part on "scope" might be helpful (e.g. uniqueness on a
timeStart, scoped to date)


2) This validaton (or any validation that is outside of the db) can still
fail when 2 separate processes (e.g. 2 Passenger processes) are
checking at the same time (a race condition). Read e.g. the section

Concurrency and integrity

in

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of

If you are building a "serious" reservation system and you do never
ever want to promise the same resource to 2 different users at the
same timeslot, you better understand that problem.


3) If your timeStart and endTime are intended as "times in a day"
and not an absolute time in history, you might use my 'relativity' gem

https://github.com/petervandenabeele/relativity

It also has a DayTimeRange that may be useful for what you are trying
to do. If you use a standardized separator you could even write an
equality or overlap comparator for DayTimeRange class.

HTH,

Peter

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to