1

I'm trying to fetch records (events) that have a date range (e.g. event_start and event_end) within a date range, so users can query all events that are between respectively inside day A and day B.

If Rails 5 would be available yet, I could use .or, but how to write this for Rails 4?

d = Date.today
Event.all.where(:date_start => d.beginning_of_week..d.end_of_week).or.where(:date_end => d.beginning_of_week..d.end_of_week)

Update

I think arel does the job.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
Slevin
  • 4,268
  • 12
  • 40
  • 90

2 Answers2

1

Something like:

Event.all.where('date_start >= ? AND date_end < ?', event_start, event_end)

You will probably need to add a day to event_end if it is the same day as the last day of your range in order for the less than not to exclude that day, e.g.:

event_end + 1.days

Another way is to use BETWEEN

Event.all.where('date_start BETWEEN ? AND ? AND date_end BETWEEN ? AND ?', event_start, event_end, event_start, event_end)
martincarlin87
  • 10,848
  • 24
  • 98
  • 145
0

Try this:-

d = Date.today
date_week = d.beginning_of_week..d.end_of_week
Event.where('date_start = ? OR date_end = ?', date_week, date_week)

Hope this may helpful!!

user3506853
  • 814
  • 5
  • 3