Reputation: 4222
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.
Upvotes: 1
Views: 46
Reputation: 814
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!!
Upvotes: 0
Reputation: 11052
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)
Upvotes: 1