Computing Ecto virtual field

Suppose I have this following schema:

schema "items" do
    field :code, :string
    field :name, :string
    field :sell_price, :decimal


I wanted to have a virtual field, where it will format my sell_price field as string. I recall, in Ecto, you can do this:

field :formatted_sell_price, :string, virtual: true

My question is, how to make Ecto always compute that field to the format that I want? Suppose I already have my own formatting code with Money or Number.

  1. When you need a computed field, you actually need a function.
  2. Functions are not data, so they should not reside in structs.
  3. If you only want that formatted_sell_price in JSON serialization, you can define your own impl of Jason.Encoder for your Item structs:
defimpl Jason.Encoder, for: Item do
  def encode(%Item{sell_price: price} = item, opts) do
    |> Map.from_struct()
    |> Map.delete(:__meta__)
    |> Map.put(:formatted_sell_price, format_price(price))

You need to replace Item with your schema module, and format_price with your own function of formatting prices.


I would like to use it in my .eex file. What’s the best way to do this? So that I can simply use something like <%= item.format_price %> and that’s it.

If I use a function there, I find that I have to call something like:

<%= ElixirApp.MasterData.Item.format_price(item) %>

Is there any way to avoid that?

1 Like

TLDR; no, not really. And overall, the explicit functional approach is preferred.

I recommend reading this thread for some good discussion on this topic: Hook function in Ecto.Schema to set virtual fields when querying a Schema struct from the data store - #8 by johmue