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: https://hexdocs.pm/ecto/Ecto.Query.html#where/3-expressions-example

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
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You’re overthinking my response, I’m saying you literally can just leave out the from:

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

Where Next?

Popular in Chat/Questions Top

xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
pietrofxq
I’ve bought the following books: Programming Elixir 1.6 Programming Phoenix 1.4 Programming Ecto Functional Web Development with Elixir...
New
OmanF
In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itse...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
pillaiindu
I am a VSCode and Sublime user and I know VIM, though I don't use VIM directly but whenever I code inside Sublime or VSCode, I use Vim em...
New
boddhisattva
Greetings everyone, At my current workplace we're evaluating different ways of building a microservices architecture for some parts rel...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
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
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement