Let's say I have the table recipes with the column ingredients, jsonb column type.
Example record:
{
id: 1,
ingredients: [
'eggs',
'fragrant bread',
'fresh tomatoes'
]
}
How can I retrieve the record with substring in where conditions?
For example:
ingredients = ['egg', 'tomato']
Recipe.where('ingredients ?& array[:keys]', keys: ingredients)
I was trying:
ingredients = ['egg', 'tomato']
Recipe.where("ingredients @> ARRAY[?]::varchar[]", ingredients).count
But I'm getting this error:
ERROR: operator does not exist: jsonb @> character varying[] (PG::UndefinedFunction)
[I]LIKE. e.g.SELECT * FROM (SELECT '["eggs", "with meatballs", "balls without meat"]'::text as col) foo where col ILIKE '%eat%'. Injecting that query to rails should be easy. Plz let me know if that works for you.