Although the context is likely irrelevant, I'm using the Chewy gem to filter Elasticsearch results using this code:
scope = scope.filter {
(send('facet_properties').send(property_ids[0], :or) == val.map(&:to_i)) |
(send('facet_properties').send(property_ids[1], :or) == val.map(&:to_i))
}
I'm looking for a way to loop through each element in property_ids
rather than calling property_ids[0]
, property_ids[1]
, etc., separated by an or
individually. In actual use property_ids
will not be a fixed length.
Not sure exactly what your structure looks like or exactly what you are trying to achieve but have you tried something like this?
vals = val.map(&:to_i)
prop_hash = Hash[property_ids.map{|prop| [prop,vals]}]
# alternately prop_hash = property_ids.each_with_object({}){|prop,obj| obj[prop] = vals}
scope.filter(facet_properties: {prop_hash}).filter_mode(:or)
Since chewy
has a #filter_mode
method to set the join type for where conditions it seems like this should work for you.