将某物存储为数组时的Rails where子句 [英] Rails where clause when something is stored as array
本文介绍了将某物存储为数组时的Rails where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PG数据库运行rails 4.2。
I am running rails 4.2, with a PG database.
我在数据库中存储了一个项目,例如(model Item
):
I have an item stored in the database such as (model Item
):
:something => ["1", "2", "3"]
我想获得 Item.where(:something.include?=> 3)
显然这不起作用-但如何
Obviously this is not working - but how are you meant to do this in rails?
推荐答案
根据 文档 ,类似这样的方法应该起作用:
According to documentation, something like this should work:
Item.where('something @> ARRAY[?]::varchar[]', ['3'])
这篇关于将某物存储为数组时的Rails where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文