acrolink
How to pass "asc" or "desc" to the Ecto.query function
I have created some basic select list (asc, desc). I want to pass either strings to Ecto.query.
# order by order_by_expires
def order_by_expires(query, nil), do: query
def order_by_expires(query, direction) do
query |> order_by(direction: :expires)
end
The above does not work, I get the error:
Ecto.Query.CompileError) expected :asc, :desc or interpolated value in order_by, got: :direction
Marked As Solved
t0t0
I think you need to write:
order_by({direction, :expires})
Else direction: is the atom :direction.
I guess direction has to be converted to atom first:
if direction in ~W[asc desc], do: String.to_existing_atom(direction), else: :asc
Or much better, do pattern matching on direction:
defp do_order_by_expires(query, direction) do
query |> order_by({direction, :expires})
end
def order_by_expires(query, "desc") do
do_order_by_expires(query, :desc)
end
def order_by_expires(query, _) do
do_order_by_expires(query, :asc)
end
2
Also Liked
josevalim
Creator of Elixir
Move the assignment out of the if:
def order_by_expires(query, direction) do
direction = if direction in ~W[asc desc], do: String.to_existing_atom(direction), else: :asc
query |> order_by({^direction, :expires})
end
You can also simplify it to:
def order_by_expires(query, direction) do
direction = if direction == "desc", do: :desc, else: :asc
query |> order_by({^direction, :expires})
end
5
acrolink
A little modification was needed to get it to work:
defp do_order_by_expires(query, direction) do
query |> order_by({^direction, :expires})
end
Thank you.
1
Popular in Questions
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
I would like to know what is the best IDE for elixir development?
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







