# Computing Ecto virtual field

**URL:** <https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715>\
**Category:** Questions\
**Tags:** ecto\
**Created:** [February 22, 2021, 5:45am UTC](https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715 "2021-02-22T05:45:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yos1p](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/yos1p/32/21891_2.png) [@yos1p](https://forum.elixirforum.com/u/yos1p)\
**Post date:** [February 22, 2021, 5:45am UTC](https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715/1 "2021-02-22T05:45:58Z")

</div>

Suppose I have this following schema:

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

    timestamps()
end

```

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:

```elixir
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`.

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [February 22, 2021, 6:10am UTC](https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715/2 "2021-02-22T06:10:31Z")

</div>

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:

```elixir
defimpl Jason.Encoder, for: Item do
  def encode(%Item{sell_price: price} = item, opts) do
    item
    |> Map.from_struct()
    |> Map.delete(: __meta__ )
    |> Map.put(:formatted_sell_price, format_price(price))
    |> Jason.Encode.map(opts)
  end
end

```

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

---

<div class="post-metadata">

**Author:** ![yos1p](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/yos1p/32/21891_2.png) [@yos1p](https://forum.elixirforum.com/u/yos1p)\
**Post date:** [February 22, 2021, 7:12am UTC](https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715/3 "2021-02-22T07:12:50Z")

</div>

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:

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

```

Is there any way to avoid that?

---

<div class="post-metadata">

**Author:** ![kip](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/32/1440_2.png) [@kip](https://forum.elixirforum.com/u/kip)\
**Post date:** [February 22, 2021, 7:26am UTC](https://forum.elixirforum.com/t/computing-ecto-virtual-field/37715/4 "2021-02-22T07:26:23Z")

</div>

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](https://forum.elixirforum.com/t/hook-function-in-ecto-schema-to-set-virtual-fields-when-querying-a-schema-struct-from-the-data-store/31622/8)
