On Fri, Dec 16, 2011 at 10:36, Javier Quarite <[email protected]> wrote:

> I have to do some validation about it, I found this
> http://stackoverflow.com/questions/5028612/reservation-type-system-w-validation-rails-3-0
>
> def uniqueness_of_date_range
>   errors.add(:start_date, "not available") unless Room.where("? >=
> start_date" AND ? <= end_date", start_date, start_date).count == 0
>   errors.add(:end_date, "not available") unless Room.where("? >= start_date"
> AND ? <= end_date", end_date, end_date).count == 0
> end
>
> but ... is there a better way?

Somewhat.  That approach could add the same error twice.  I'd probably do it as:

  errors.add(:start_date, "not available") unless
    (Room.where("? >= start_date AND ? <= end_date",
                start_date, start_date).count == 0 and
     Room.where("? >= start_date AND ? <= end_date",
                end_date, end_date).count == 0)

(Basically, instead of "error unless x" and then "same error unless
y", use "error unless x and y".)

Also I tend not to use "unless" very much; I don't think this
particular usage is any clearer than using "if" and checking whether
count > 0.  But that's just a matter of personal style.

Other than that, yes, it's pretty much what I was thinking.

-Dave

-- 
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

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