yos1p

yos1p

Suppose I have this following schema:

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:

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.

Showing Posts 1 to 3

Aetherus

Aetherus

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

yos1p

yos1p OP

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?

kip

kip

ex_cldr Core Team

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews