Implementation Of Blocked Dates For A User-event Orm Model
In continuation of Find entry where value does not intersect with other value I have an application (Ruby on Rails, ActiveRecord, Postgresql) that uses a user table, date table and
Solution 1:
Here is the solution I chose. The trick was using left outer join (ruby command eager_load) for users and blocked_date_periods table, and including those users whose start_date field in the joined table is NULL, obviously because they do not have any blocked date objects associated with themselves. The query I use:
User.eager_load(:blocked_date_periods).
where("blocked_date_periods.start_date is null OR
not tsrange(
blocked_date_periods.start_date - '00:59:59'::interval,
blocked_date_periods.end_date + '00:59:59'::interval
) @> ?::timestamp",
Date.parse(DATE_STRING)).count
I had to add and subtract 1 hour from the start and end date because the query did not want to encompass exact end dates for some reason, so that 12-26-2015 was not included inside the period of from 12-22-2015 to 12-16-2015 for some reason I am yet to understand.
For some reason I do not like that solution and would like to know whether there is a query that is better and faster than what I have.
Post a Comment for "Implementation Of Blocked Dates For A User-event Orm Model"