A
A
Andrey2017-11-16 14:03:43
PostgreSQL
Andrey, 2017-11-16 14:03:43

How to combine two where queries (from an array) into one?

I am doing a search (autocomplete), I need to find the name Tire TYUMEN 175/70 on request tire 175 .
I do with ILIKE
How to get a request like

SELECT "products".* FROM "products"  WHERE (title ILIKE '%шина%') AND (title ILIKE '%175%')
?
Rails version 3 , so do:
products = Product.all
["шина", "175"].each do |word|
  products = products.where("title ILIKE ?", "%#{word}")
end

also not an option.
or if without Product.all, then get a request
SELECT "products".* FROM "products"  WHERE (title ILIKE '%175')

PS It is possible to build a query using SIMILAR TO, but it doesn't work here either.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitri Sinitsa, 2017-11-16
@andreychumak

Harness the power of Arel table, <%=username%>

q = Product.arel_table[:title].matches_all(["шина", "175"])
products = Product.where(q)

B
Boris Korobkov, 2017-11-16
@BorisKorobkov

like %...%- it's a crutch.
Then you need to find "tire" by "tire".
Then, so that the "bus" is NOT "car".
Then - the ranking of the results.
And it will turn out to be shit code, which will only have to be thrown out entirely.
Why not just write right?
https://www.postgresql.org/docs/current/static/tex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question