Cláudio Júlio Cláudio Júlio - 17 days ago 7
JSON Question

How to pass values in JSON for the Ecto Query?

The client sends the following JSON: {"user": {"age+":18,"age-":40}}

In my controller I have the following:

def procura(conn, query) do
maior = conn.params["user"]["age+"]
menor = conn.params["user"]["age-"]
query = from u in query, where: u.age > ^maior and u.age < ^menor, select: u.name
pesquisa = Repo.all query
IO.puts pesquisa
end


But I got the compiler warning:
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for %{}


How do I pass the values in JSON for the Ecto query?

My goal is to do a query based on JSON fields. I want to query for users who have between x and y years.
Automatically translated.

Answer

What you might wanted to do is:

In my controller I have the following:

def procura(conn, %{"user" => %{ "idade+" => maior, "idade-" => menor }}) do
  query = from u in Module.Model, where: u.idade > ^maior and u.idade < ^menor, select: u.name
  pesquisa = Repo.all query
  IO.puts pesquisa
  text conn, "Works"
end