0

Appointments have two fields, an at (a datetime) and minutes (integer, length of appointment)

appointment_a: at = "2015-04-02 21:00:00", and minutes = 60, which already exists.

I'm trying to create appointment_b, where at = "2015-04-02 21:30:00", and minutes = 60.

This is a validation in the Appointment model:

def check_overlap
  from = appointment_a.at 
  to = appointment_a.minutes * 60 + appointment_a.at
    if Appointment.where('at >= ? AND at + minutes * 60 >= ?', from, to).emtpy? == false 
      errors.add(:at, (" field will cause overlap between existing appointments "))
    end
end 

Why is this not adding errors?

1 Answer 1

1

If you try to add a Time to an Integer you get an error:

Time can't be coerced into Fixnum

Maybe if you change your to variable declaration to this:

to = appointment_a.at + appointment_a.minutes * 60

This way, you're adding an Integer to a Time type, so there should be no errors

Sign up to request clarification or add additional context in comments.

2 Comments

that's already what I do in my system, I didn't know the order mattered. Any other possible solution ?
I think you're trying to check for overlap in the wrong way, overlap happens when either of these two happen: there's another appointment that starts later but ends earlier or there's one that starts earlier and ends later. The way you're checking it, the query is checking if there's an appointment that starts after appointment b, and there isn't since appointment a starts before b. You need to rewrite the query to match your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.