0

For reference, this question was boggled, so thanks to everyone who helped unboggle it.

I'm trying to locate potential collisions of events in a booking program where events have varying lengths.

(a ≤ x && e ≥ y) || (a ≥ x && e ≤ y) || (a ≤ x && e ≤ y) || (a ≥ x && e ≥ y)

I'm using DataMapper to accomplish this, but the query I had before was wrong. I then figured out the above, but I'm not sure how to translate that into a query. Below is my incorrect query for reference.

class Table
  def is_available?
     return false if (TableBooking.all(:at.lte => params[:at], :at.gt => params[:ending], :ending.gt => params[:at], :ending.gte => params[:at]).count > 0)
  end
end
arbales
  • 5,466
  • 4
  • 33
  • 40
  • The comparison makes no sense to me - aside from the x & y points, the first two comparisons are mutually exclusive. The subsequent tests check other ranges, but one of the four will return true. – OMG Ponies Jan 02 '10 at 23:04
  • exactly my thoughts OMG, that test is always true! – Paul Creasey Jan 02 '10 at 23:08

2 Answers2

2

I think what you actually want is this:

(a ≤ x && e ≥ y) || (a ≥ x && e ≤ y) || (a ≤ x && e ≥ x) || (a ≤ y && e ≥ y)

Since that accounts for (corresponding to each group in order above)...

  1. a,e includes x,y
  2. a,e is included in x,y
  3. a,e starts before x,y but doesn't finish before x,y
  4. a,e ends after x,y but doesn't start after x,y

The first two portions are the same as your original, but I had to change the second two portions to make it make sense and be correct.

This can actually be simplified as well, because #3 and #4 will automatically catch #1, so you really only need #2-4:

(a ≥ x && e ≤ y) || (a ≤ x && e ≥ x) || (a ≤ y && e ≥ y)
Amber
  • 507,862
  • 82
  • 626
  • 550
0

The inequality you gave can be simplified to

(a ≤ x || a ≥ x) && (e ≤ y || e ≥ y)  

which can be further simplified to

True

That is probably why the code isn't working.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283