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
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
def order_by_expires(query, direction) do
if direction in ~W[asc desc], do: String.to_existing_atom(direction), else: :asc
query |> order_by({^direction, :expires})
end
I get this error message:
expected :asc or :desc in order_by, got: "asc" on line:
I had to rewrite the single method solution like this in order for it to work:
def order_by_expires(query, direction) do
if direction in ~W[asc desc], do: direction = String.to_existing_atom(direction), else: direction = :asc
query |> order_by({^direction, :expires})
end
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