1

I need make a query on Rails where select records comparing two attributes of two record

for example:

SELECT p.id, u1.id, u2.id FROM User u1, User u2, person p 
WHERE u1.weekDay = u2.weekDay AND u2.initialTime - u1.endTime < 0 
AND u2.id != u1.id AND u1.initialTime < u2.initialTime

Specifically at this part where u2.initialTime - u1.endTime < 0, how can I puts 2 object in a where(endTime: :initialTime) ?

Thank.

Deval Khandelwal
  • 3,458
  • 1
  • 27
  • 38
smoK
  • 55
  • 1
  • 7

1 Answers1

1

where u2.initialTime - u1.endTime < 0

You can refactor this to:

u2.initialTime < u1.endTime as a-b<0 is equivalent to a < b:

http://www.wolframalpha.com/input/?i=a-b+%3C0&x=0&y=0

Also you can use where to perform comparisons like:

User.where('created_at < updated_at')
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115