I'm using postgres database and trying to query all records with "Value"=>"Black" in this JSONB field. That field contain an array of objects, e.g. {"id"=>"1", "key"=>"size", "value"=>"P"}
How do I query(case-insensitive) this records?
This is my code so far
def by_feature_value(value)
relation.where('features @> ?', [{ value: value }].to_json)
end
Records
#<ProductSku:0x000055de9cc01ba8
id: 33,
product_id: 3,
code: "1234",
ean: "12345",
created_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
updated_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
features: [{"id"=>"2", "key"=>"Color", "Value"=>"Black"}]>
#<ProductSku:0x000055de9cc01ba8
id: 33,
product_id: 3,
code: "1234",
ean: "12345",
created_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
updated_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
features: [{"id"=>"2", "key"=>"Color", "Value"=>"black"}]>,
The correct answer to this is
ProductSku.where('lower(features::text)::jsonb @> lower(?)::jsonb', [{ Value: value }].to_json)
Without type casting the first comment on your post does not work.