Ecto Query as Map and All fields

Hi, is there a way to get the query in a map with all its fields and not declare it one by one:

select: map(q, [:id, :statement])

  def show(conn, %{"id" => chapter_id}) do
    questionsQuery = from q in Question, where: q.chapter_id == ^chapter_id, select: map(q, [:id, :statement])
    questions = Poison.encode!(Repo.all(questionsQuery))
    render(conn, "show.html",  questions: questions)
  end
3 Likes

I’m interested in this as well.

Isn’t the default that is returned when not using select a struct which contains all the fields? If you want to remove the struct name, run Map.from_struct/1 on it afterwards.

1 Like

The exact API in the post is supported: https://hexdocs.pm/ecto/Ecto.Query.API.html#map/2 (maybe not at the time the post was written though - I am too lazy to check it).

I think the OP is objecting to the API that he has presented. He doesn’t like needing to specify the keys.

Oh, thank you! @Qqwy is correct then!