如何在类中找到hstore键不存在或如果存在则不为零的所有对象?
产品表有:image_urls
的hstore
t.hstore "image_urls", default: {}
这将获得未为image_urls
:设置有用值的所有产品
Product.where(image_urls: ['', nil])
但是,如果我想找到所有产品image_urls[:thumbnail]
为零或不存在的产品,该怎么办?
这不起作用:
Product.where(image_urls[:thumbnail]: ['', nil])
我认为您正在寻找以下运算符:
hstore ? key # does hstore contain key?
hstore @> hstore # does left hstore contain right?
因此,最终的解决方案是:
Product.where("not image_urls ? 'thumbnail' or not image_urls @> 'thumbnail=>NULL'")