ids = [123, 4, 1, 3, 2, 456] posts = from(p in Post, where: p.id in ^ids, select: {p.id, p}) |> Repo.all |> Map.new posts = for id <- ids, posts[id], do: posts[id] posts |> Enum.map(&(&1.id)) |> IO.inspect