wolfiton

wolfiton

Rewriting this where using private fn

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

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

t is not a real variable, because where is not a function, it’s a macro. t is part of the syntax of the macro. Examples here: Ecto.Query — Ecto v3.14.0

The where macro basically just creates a t that you can use in the second part of where.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yes. If you pipe a query struct into where, it grabs the schema you’re querying out of the query and makes it available as whatever variable you put in the list.

wolfiton

wolfiton

Final version using the advice from @benwilson512

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
    |> where([t], ilike(t.name, ^"%#{name}%"))
  end

Where Next?

Popular in Chat/Questions Top

mchean
I was wondering if anyone is using some learning tools that they can recommend? I’ve been looking at two specifically so would also be ...
New
phykos
In Ruby, which is very similar to Elixir I do this: def test yield end test do puts("sup there") end Here, the yield keyword will be...
New
KSingh1980
How to run the random query and get the result in JSON format and send it as a response. The query will be in the following form DB Name...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
gouvermxt
I just finished the “Learn Functional Programming with Elixir (Pragprog)” book. I have 5+ years of experience with Ruby/Rails, my goal is...
New
jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
Fl4m3Ph03n1x
Background I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows: Create an impl...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement