像和阵列一样的导轨 [英] Rails where LIKE and array

查看:77
本文介绍了像和阵列一样的导轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做?

myarray = ["name1","name2"]
Product.where('name ILIKE ?', %#{myarray}%)

我需要获取名称类似于 name1 name2 的所有产品。

I need to get all products where names are like name1 and name2.

这可能吗?

推荐答案

我认为您想使用 ILIKE 函数。

I think You want to test all values with ILIKE function.

在Postgres中是这样的:

This is how it's done in Postgres:

select * from table where value ilike any (array['%foo%', '%bar%', '%baz%']);

尝试像这样转换为Rails / Ruby语法:

Try to convert to Rails/Ruby syntax like this:

myarray_with_percetage_signs = ["name1","name2"].map {|val| "%#{val}%" }
Product.where("name ILIKE ANY ( array[?] )", myarray_with_percetage_signs)

这篇关于像和阵列一样的导轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆