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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews