Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

380
Visualizações
Rails/PSQL - turning a column table into an array and search if a subarray is included

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!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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}")
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda