1
如何查找某個類中hstore密鑰不存在或存在時不爲零的所有對象?Rails - 查找hstore密鑰不存在或=無的所有記錄
產品表有:image_urls
t.hstore "image_urls", default: {}
的hstore這將讓其中沒有有用的值被設置爲image_urls
所有產品:
Product.where(image_urls: ['', nil])
但是如果我想找到的所有產品產品image_urls[:thumbnail]
是零還是不存在?
這不起作用:
Product.where(image_urls[:thumbnail]: ['', nil])
我猜測你需要查看PostgreSQL文檔:http://www.postgresql.org/docs/9.4/static/hstore.html#HSTORE-OP-TABLE –
謝謝!這導致我找到了解決方案。如果你想在下面發佈答案,我會給你信用:'Product.where(「image_urls @>'thumbnail = NULL'」)' – Meltemi
仍然希望找到這兩個鍵缺少OR鍵= NULL。你可以結合ActiveRecord查詢與SQL? – Meltemi