Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用通配符对数组列进行Activerecord查询_Ruby On Rails_Ruby_Postgresql_Activerecord - Fatal编程技术网

Ruby on rails 使用通配符对数组列进行Activerecord查询

Ruby on rails 使用通配符对数组列进行Activerecord查询,ruby-on-rails,ruby,postgresql,activerecord,Ruby On Rails,Ruby,Postgresql,Activerecord,假设我有一个客户型号,带有数组列电话。 使用给定的电话很容易找到所有客户 Customer.where('?=任何(电话)'+79851234567') 但当我想找到与给定手机相似的客户时,我不知道如何使用通配符,比如: Customer.where('ANY(phone)LIKE?','+7985%') 我正在使用PostgreSQL 9.5和Rais 4.2 有什么想法吗?我认为,首先,最好使用带有customer\u id、phone\u number字段的第二桌电话。我认为这更像是一

假设我有一个
客户
型号,带有数组列
电话
。 使用给定的电话很容易找到所有客户

Customer.where('?=任何(电话)'+79851234567')
但当我想找到与给定手机相似的客户时,我不知道如何使用通配符
,比如:

Customer.where('ANY(phone)LIKE?','+7985%')
我正在使用PostgreSQL 9.5和Rais 4.2


有什么想法吗?

我认为,首先,最好使用带有customer\u id、phone\u number字段的第二桌电话。我认为这更像是一种方式)。通过这种方式,您可以使用此查询

Phone.where("phone_number LIKE ?", '%PART%').first.customer
如果在某个文本字段(例如JSON)中序列化数组,则应在模式的两侧使用%:

Customer.where('phones LIKE ?', '%+7985%')
如果数据库中有一个数组,则应使用unest()函数将数组扩展为一组行。

您能试试吗

Customer.where("array_to_string(phones, ', ') like ?", '+7985%')

我相信这会奏效

你在用什么数据库?@CdotStrifeVII他在用postgres。检查标记。@CdotStrifeVII Postgresql。我在标签中写了它,但忘了在帖子中提到。
ANY
用于数组比较()。你想干什么?找到有这种模式的电话号码的客户?@lcguida完全正确。
“%+7985%”
将是正确的方法,我相信。对于像
['+7921123456'、'+7985123456']这样的情况