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

First 10 of 31 Posts Switch mode

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

krisleech
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban). For this kind of work you do not tend, in my e...
New
mhindujadheerajsudan
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement