You can do whatever you want within the where
block; yet you must return the actual query at the end, because this is what will be used as the where clause.
So, given an array of times like this:
times = [ [ '09:00:00', '12:00:00' ], [ '18:00:00', '23:00:00' ] ]
Here is a verbose solution:
Planning.where do
query = nil
times.each do |a,b|
q = (start_time >= a) & (end_time <= b)
if query
query |= q
else
query = q
end
end
query
end
Here is a more clever solution:
Planning.where do
times.map { |a,b| (start_time >= a) & (end_time <= b) }.reduce(&:|)
end
Both do generate the following SQL:
SELECT "plannings".* FROM "plannings"
WHERE ((
("plannings"."start_time" >= '09:00:00' AND "plannings"."end_time" <= '12:00:00')
OR
("plannings"."start_time" >= '18:00:00' AND "plannings"."end_time" <= '23:00:00')
))