简体   繁体   中英

Rails: Querying a field that contains array

Created a table clients using Postgres database having an order_ids field which is an array field ( array: true ). I want to fetch only those clients whose order_ids field contains value '220'.

Example: order_ids contains ["12","13","220"] . So, in where clause I want to do something like this:

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

Any idea?

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

Rails文档中的更多用法示例

Have you try like this :

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

Or

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

or

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

You can find details here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM