Rails:查询包含数组的字段

时间:2017-05-02 12:03:34

标签: ruby-on-rails

使用具有clients字段的Postgres数据库创建表order_ids,该字段是数组字段(array: true)。我想只获取order_ids字段包含值'220'的客户端。

实施例: order_ids包含["12","13","220"]。所以,在where子句中我想做这样的事情:

Client.where("order_ids contains(?)",220)

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

Client.where("220 = ANY(order_ids)")

Rails documentation

中的更多用法示例

答案 1 :(得分:0)

你试试这样:

Client.where("? = ANY(order_ids)", 220)

Client.where("order_ids @> ?", '{202}')

Client.where("? = ANY(order_ids)", '{220}')

您可以找到详细信息herehere

相关问题