yos1p
Computing Ecto virtual field
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.
Most Liked
kip
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
Aetherus
- When you need a computed field, you actually need a function.
- Functions are not data, so they should not reside in structs.
- If you only want that
formatted_sell_pricein JSON serialization, you can define your own impl ofJason.Encoderfor yourItemstructs:
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
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?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









