Ruby中搜索结果的交集

时间:2022-09-10 19:55:16

I am building a product search that is display in a Ruby on Rails application. My original model looked like this:

我正在构建一个在Ruby on Rails应用程序中显示的产品搜索。我的原始模型看起来像这样:

def candidates  
  @search ||= find_product.items
end 

def store
  .... Store Client ....
end 

private

def find_products
  products = store.search_products('location:"#{location}"', order: "desc") if location.present?
  products = store.search_products("#{size} ", order: "desc") if size.present?
  products = store.search_products("#{brand}", order: "desc") if brand.present?
  products
 end

But I thought it would be better to take the intersection of the results of from each of the attributes.

但我认为从每个属性的结果中获取结果会更好。

private


def find_products_by_location
  products = store.search_products('location:"#{location}"', order: "desc") if location.present?
end

def find_products_by_size
   products = store.search_products("#{size} ", order: "desc") if size.present?
end

def find_products_by_brand
  products = store.search_products("#{brand}", order: "desc") if brand.present?
end

def find_product
  find_products_by_location & find_products_by_size & find_products_by_brand
end

Unfortunately when I test in the console I get a returned value of true or false. Each individual attribute has an array of data if it is present in the form.

不幸的是,当我在控制台中测试时,我得到了返回值true或false。如果表单中存在数据,则每个单独的属性都有一个数据数组。

How can I get results of the intersection of multiple arrays?

如何获得多个数组交集的结果?

1 个解决方案

#1


1  

You always need to return an Array (or Set) from each of your methods:

您总是需要从每个方法返回一个数组(或Set):

def products_by_location
  if location.present?
    store.search_products('location:"#{location}"', order: "desc")
  else 
    []
  end
end

def products_by_size
  if size.present?
    store.search_products("#{size} ", order: "desc")
  else
    []
  end
end

def products_by_brand
  if brand.present?
    store.search_products("#{brand}", order: "desc")
  else
    []
  end
end

def find_product
  products_by_location & products_by_size & products_by_brand
end

You could also leverage some metadata-programming here:

您还可以在此处利用一些元数据编程:

def products_by(key)
  if params[key].present?
    store.search_products("#{key} ", order: 'desc')
  else
    []
  end
end

def products
  products_by(:location) & products_by(:size) & products_by(:brand)
end

Lastly, you should really consider pushing this code down into the model level. Having this type of logic in the controller is generally considered a bad pattern.

最后,您应该考虑将此代码推送到模型级别。在控制器中具有这种类型的逻辑通常被认为是坏模式。

#1


1  

You always need to return an Array (or Set) from each of your methods:

您总是需要从每个方法返回一个数组(或Set):

def products_by_location
  if location.present?
    store.search_products('location:"#{location}"', order: "desc")
  else 
    []
  end
end

def products_by_size
  if size.present?
    store.search_products("#{size} ", order: "desc")
  else
    []
  end
end

def products_by_brand
  if brand.present?
    store.search_products("#{brand}", order: "desc")
  else
    []
  end
end

def find_product
  products_by_location & products_by_size & products_by_brand
end

You could also leverage some metadata-programming here:

您还可以在此处利用一些元数据编程:

def products_by(key)
  if params[key].present?
    store.search_products("#{key} ", order: 'desc')
  else
    []
  end
end

def products
  products_by(:location) & products_by(:size) & products_by(:brand)
end

Lastly, you should really consider pushing this code down into the model level. Having this type of logic in the controller is generally considered a bad pattern.

最后,您应该考虑将此代码推送到模型级别。在控制器中具有这种类型的逻辑通常被认为是坏模式。