Finding records that overlap a range in Rails

前端 未结 2 1772
暗喜
暗喜 2020-12-15 22:23

So, I have an Event model that has a starts_at and a ends_at column and I want to find events that take place in a range of dates.

2条回答
  •  孤街浪徒
    2020-12-15 23:10

    I guess an event overlaps if it begins before the range ends and ends after the range begins. :)

    So:

    {:conditions => ['? < ends_at AND ? > starts_at', range.first, range.last]}
    

提交回复
热议问题