I am totally stuck on this issue and I cannot find any answer on my doubt.
I will try to explain in the best way possible but that's pretty hard.
So, I have got a first model(Recipe), that stores inside the column ingredients all the ingredients inside an array. I also have an Ingredients model, which is connected through a join table to Recipe, that has as the first column the names of all the ingredients available.
I would like to include inside my search method a query that turns the ingredients.name column into an array and return back from the search method only those recipes whose array of ingredients is fully contained in ingredients.name array.
I tried with this
recipes = recipes.joins(:ingrediantizations).where('array_agg('ingredients.name') @> recipe.ingredients')
But it is not giving the right resuslt - ingrediantizations is the join table.
Hope you can help me out!
After a couple of day of wrapping my head around this, I finally got to the solution.
I post it so that someone could benefit for it.
The second step could seem weird but it is, I guess, the only way to make an array with curly brackets, as required for literal arrays in PSQL.
Ingredients was the attribute storing the ingredients in an array.
items = Item.pluck(:name)
items = "{#{ items.map {|term| %Q("#{ term }") }.join(",") }}"
recipes=recipes.where("ingredients <@?", "#{items}")