Map, filter and reduce

You don’t want to do Repo.all inside the Enum.reduce callback. The idea is that reduce is reducing over a single query, building it up with each iteration. The return value of the Enum.reduce is the query that you want to execute.

user_params = [%{"age" => 19}, %{"name" => "matt213"}, %{"sex" => "male"}]
query =
  user_params
  |> Enum.map(&Enum.to_list/1)
  |> Enum.reduce(Foo, fn [{key, value}], query ->
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)
  end

users = Repo.all(query)

Or simply:

users =
  user_params
  |> Enum.map(&Enum.to_list/1)
  |> Enum.reduce(Foo, fn [{key, value}], query ->
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)
  end
  |> Repo.all