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
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.
4
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.
3
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
3
Popular in Chat/Questions
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
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
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
Hi all,
I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
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
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
I just finished the “Learn Functional Programming with Elixir (Pragprog)” book. I have 5+ years of experience with Ruby/Rails, my goal is...
New
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
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
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...
New
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
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
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
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








