jerry

jerry

Ecto query using like/ilike in query

Good day to you all.

I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?

product="SurfacePro"

from(u in Product,
  where: like(u.product_name, %product%),
  select: %{product_id: u.id, description: u.product_desc}
) |> Repo.all

Most Liked

NobbZ

NobbZ

Have you taken a look at the documentation? https://hexdocs.pm/ecto/Ecto.Query.API.html#like/2

From how I read it, you need to pass in a string as second argument, eg. like(u.product_name, "%SurfacePro%"), but you wan’t this probably set dynamically at runtime from user input.

Since I’m not sure if you are allowed to interpolate a string in the query the way you would do it outside, I’ll prepare it and then pin:

product = "SurfacePro" # or from a function argument?
like = "%#{product}%"

from(u in Product,
  where: like(u.product_name, ^like),
  select: %{product_id: u.id, description: u.product_desc}
) |> Repo.all
12
Post #2
jerry

jerry

Hello @NobbZ,

Many thanks.
Your suggestion worked.

I had tried several different options without success.

Regards,

Jerry

rameshsharma

rameshsharma

Hi Luca,

Thanks for responding. I posted here as I thought it could be linked to like search. S.orry for that
The roles column in database is a Postgres array of strings. You are correct.
I want to get the count of rows that include ‘super’ among the roles?

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement