The Missing Bit

Selecting virtual field in Ecto
February 21, 2017

In you Ecto model, you might have some virtual fields that needs to be computed when you select from your database.

The select option of from will override the whole set of returned fields. Which means if you do something like:

from d in Document, select: %{shared: true}

You will get only an array of map with %{shared: true} as content.

The trick to add a field, is to use regular elixir syntax, like so:

from d in Document, select: %{d | shared: true}

Of course, replace shared with whatever SQL function you want.