wolfiton

wolfiton

I have the following function that I want to rewrite using the defp

def list_items(%{matching: name}) when is_binary(name) do
    Item
    |> where([m], ilike(m.name, ^"%#{name}%"))
    |> Repo.all()
  end

What I tried

def list_items(%{matching: name}) when is_binary(name) do
    Item
    |> search(term)
    |> Repo.all()
  end

  defp search(term) do
    term
    |> from t in Item
    |> where([t], ilike(t.name, ^"%#{term}%"))
  end

Can someone explain how to do this?

from` must be a compile time keyword list

Shouldn’t Item work here?
Thanks

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
    term
    |> from t in Item

This isn’t a valid way to use from, it expands to from(term, t in Item) since |> puts arguments at the front.

wolfiton

wolfiton OP

What can I pipe it through?

Kurisu

Kurisu

When you’re using from you don’t pipe.

See this topic:

#keywords query syntax
import Ecto.Query
from u in “users”, where: u.age > 18, select: u.name

#pipe-based syntax
“users”
|> where([u], u.age > 18)
|> select([u], u.name)
wolfiton

wolfiton OP

This works

def list_items(%{matching: name}) when is_binary(name) do
    Item
    # |> where([m], ilike(m.name, ^"%#{name}%"))
    |> search(name)
    |> Repo.all()
  end

  defp search(query, name) do
    query = from(query in Item)

    query
    |> where([t], ilike(t.name, ^"%#{name}%"))
  end

But I get a dializer error with unused query variable. Is there a better way to do this?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You can’t pipe into from. The ecto docs contain examples of how to use pipe with where, you don’t need to use from.

wolfiton

wolfiton OP

I couldn’t think of another way that is why, I am asking.

I also read through the docs.

tenzil

tenzil

Hi , I kind of did this for my requirement. hope it helps you,

def search(params)
 qfilters       = reject_empty_values(params)
search_q =  profile_q(qfilters)
(from p in search_q , where: p.id == 2) |> Repo.all
end
defp profile_query([]), do: Profile
defp profile_query(filters) do
  from q in Profile, where: filters
end
wolfiton

wolfiton OP

Thanks but i am trying to keep the public fn clean and use only pipes there and do the actual work in private fn.

tenzil

tenzil

How about this

def search(model, %{name: name} = params) do
   model
   |> search_q(name)
   |> Repo.all
end

defp search_q(model, name) do
  from m in model, where: m.name == name
end
wolfiton

wolfiton OP

Interesting, I am now wondering if if can keep and do

def list_items(%{matching: name, query: query}) when is_binary(name) do
    Item
    # |> where([m], ilike(m.name, ^"%#{name}%"))
    |> search(name)
    |> Repo.all()
  end

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews