[英]How to create { x = y | y = z | z = 0 } block dynamically from hash?
Although the context is likely irrelevant, I'm using the Chewy gem to filter Elasticsearch results using this code: 尽管上下文可能无关紧要,但我正在使用Chewy gem通过以下代码过滤Elasticsearch结果:
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. 我正在寻找一种方法来遍历
property_ids
每个元素,而不是调用由a or
单个分隔的property_ids[0]
, property_ids[1]
等。 In actual use property_ids
will not be a fixed length. 在实际使用中,
property_ids
不会是固定长度。
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. 由于
chewy
有一个#filter_mode
方法来为条件设置连接类型,因此这似乎对您有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.