简体   繁体   English

如何创建{x = y | y = z | z = 0}从哈希动态阻止?

[英]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.

相关问题 如何在ActiveRecord准备好的语句中使ruby数组为ax,y,z - how to make a ruby array be a x,y,z in an ActiveRecord prepared statement [X,Y,Z] .each {| m |的区别是什么? 包括m}并包括X,Y,Z? - What is the difference between [X,Y,Z].each {|m| include m} and include X, Y, Z? Ruby语法:x('foo',y:'bar',z:'baz')是否可以不同于x('foo',@yz)? - Ruby syntax: can x('foo', y: 'bar', z: 'baz') differ from x('foo', @yz)? Z = X + Y时如何防止堆叠顺序? X总是在最前面 - how to prevent stacked ordering when I do Z=X+Y? X is always on top rails:获取标记为x AND y AND z的所有项目 - rails: Get all items tagged x AND y AND z 如何查询 ActiveRecord 以仅返回每个 object 的第一个实例,其中 enum = X、Y、Z? - How do I query ActiveRecord to return only the first instance of each object where enum = X, Y, Z? 如何根据滑轨中的(表Y /列Z)定义(表A /列X)? - How to define (table A/column X) according to (Table Y/column Z) in rails? 模型X和模型Y每个has_many Z,:dependent =>:destroy。 如果我摧毁X,Z会被摧毁吗? - Model X and Model Y each has_many Z, :dependent => :destroy. If I destroy X, does Z get destroyed? 会话未创建例外:使用Selenium Webdriver和Chrome时,Chrome版本必须> = xyz - Session not created exception: Chrome version must be >= x.y.z when using Selenium Webdriver with Chrome Ruby:now.strftime(“%a,%d%b%Y%X%z”)给出了错误的时间 - Ruby: now.strftime(“%a, %d %b %Y %X %z”) gives wrong time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM