In this talk on Ecto the speaker recommends to calculate persistent fields from virtual Ecto.Schema fields that come in from user input in a changeset.
In his example there is an Ecto.Schema like
defmodule MusicDb.Track ... schema "tracks" do field :title, :string field :duration, :integer field :duration_string, :string, virtual: true timestamps() end
duration field holds the duration in seconds persistently in the database. The
duration_string is used to hand in the duration time from user input as a string, For example
duration_string comes in as
"3:25" in the changeset which would be piped through a function that sets the
time_in_seconds field to 205.
So far so good. But how do I go the way back? When I query a
duration_string field would be
nil. Is there any way to install some hook function in the schema where the
Track struct is piped through whenever I query a
Track so that I can calculate and set