Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!