I have an order that contains multiple bookings and I am trying to apply select condition on the bookings that specify certain condition specified in the hash in the following way
{ status: 'confirmed',
slot: '1 p.m. - 2 p.m.'}
I need to make a generic function that works on bookings for any number of filters that satisfy the conditions.In this case the condition is {slot '1 p.m. - 2 p.m.' and status is confirmed}
I made the following function for this but was not able to give the && conditions from filters
def bookings_of(filters)
self.bookings.select do |booking|
filters.map{ |key, value| booking.send(key) == value }
end
end
How should I do this?
If you really wanted to do this in Ruby you could do:
def bookings_of(filters)
self.bookings.select do |booking|
!!filters.map { |key, value| break if booking.send(key) != value }
end
end
Using break here will bail as soon as one of the filters does not pass and it uses the fact that nil is falsy while an array is truthy.
But you should probally just use .where
and do it in the database instead.
In which case you can do it in one call by merging the array of hashes into a single hash:
bookings.where(filters.reduce({}, :merge))