查询int是否在数组列内,Postgres with Rails
我有一个表格,其中包含整数数组类型的列。 给定一个整数作为输入,我想查询名为tipo_propiedad的列包含输入整数的行。 直接在Postgres中,可以使用&&运算符并将输入int转换为数组。 问题是使用这个:
Zone.where("tipo_propiedad && ?", [].push(type)).count
其中type是输入整数,给出
SELECT COUNT(*)FROM“zonas”WHERE(tipo_propiedad && 1)PG :: UndefinedFunction:ERROR:operator不存在:integer [] && integer
我也尝试过
Zone.where("tipo_propiedad && ?", [5].push(type))
因为tipo_propiedad里面只有1,2或3个而且给出了
SELECT COUNT(*)FROM“zonas”WHERE(tipo_propiedad && 5,1)PG :: UndefinedFunction:ERROR:operator不存在:integer [] && integer
我也在使用squeel但是gem没有任何操作符来执行此操作。
你也可以
Zone.where("'?' = ANY (tipo_propiedad)", 1)
要么
Zone.where("tipo_propiedad && ARRAY[?]", 1) # or "&& '{?}'"