Sunday, 29 September 2013

How to use ActiveRecord "where" query to check overlap between two arrays

How to use ActiveRecord "where" query to check overlap between two arrays

I have a Meetings class which contains a 'days' attribute, which I've
serialized into an Array. So for example, running:
"Meeting.first.days"
might return:
'["Monday", "Tuesday", "Wednesday"]'
I have a search form where users can check which days they want to search
for a meeting. So a user might want to see any meetings whose days include
Monday. I'm having trouble using ActiveRecord to filter these meetings. So
far I have:
meetings = Meeting.order(:start_time)
meetings = meetings.where("days in (?)", days_params)
But this keeps filtering the 'meetings' variable down to 0 results. I
could be wrong but I think the problem is that this only works if 'days'
is not an array, i.e. if I compare a string with an array then it might
work. Since I need to compare the union of two arrays, anyone have an
idea?

No comments:

Post a Comment