Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda