hudsonbay

hudsonbay

Hello, I have a very newbie question if anyone knows.

I’m using this libraries

{:ex_money, "~> 5.5.1"},
{:ex_money_sql, "~> 1.4.4"}

to deal with Money. First time I work with Money types because I was doing it using only decimals.

I configured this libraries well, I know because I can seed data with no problem using this:

Repo.insert!(%InnovationProject{
  budget: Money.new(:USD, 7800),
  term: 2
})

and receiving no errors at that time. And when doing GET requests the format is a Money type in the result.
So, with that out of the way, I have the following Ecto.Schema on Phoenix Framework:

schema "innovation_projects" do
    field :budget, Money.Ecto.Composite.Type
    field :term, :decimal
    field :anual_cost, Money.Ecto.Composite.Type, virtual: true

    timestamps()
  end

My changeset:

def changeset(innovation_project, attrs) do
    innovation_project
    |> cast(attrs, [
      :budget,
      :term
    ])
    |> validate_required([
      :budget,
      :term
    ])
    |> calculate_anual_cost()
  end

And calculate_anual_cost function looks like this:

 defp calculate_anual_cost(changeset) do
    budget = get_change(changeset, :budget)
    term = get_change(changeset, :term)

    changeset
    |> put_change(:anual_cost, budget |> Money.div(term))
  end

Now, I think that there’s no problem here. But my error comes when I want to show anual_cost on my views

So, my innovation_project_view.ex looks like this:

def render("innovation_project.json", %{innovation_project: innovation_project}) do
    %{
      budget: innovation_project.budget,
      term: innovation_project.term,
      anual_cost: innovation_project.anual_cost
    }
  end

Eyes on anual_cost: innovation_project.anual_cost because that’s the line giving me this issue:

protocol Jason.Encoder not implemented for {:ok, #Money<:USD, 20000>} of type Tuple, Jason.Encoder protocol must always be explicitly implemented. This protocol is implemented for the following type(s): Money, Cldr.LanguageTag, Ecto.Association.NotLoaded, Ecto.Schema.Metadata, BitString, List, Integer, Any, Date, Map, NaiveDateTime, Atom, Jason.Fragment, DateTime, Decimal, Time, Float

So, my view can show budget, which is a Money type but the generated virtual field anual_cost can’t be shown. How can I show my anual_cost?

One more thing:

iex> Money.div Money.new(:USD, 200), 2
    {:ok, Money.new(:USD, 100)} 

Thanks in advance

Showing Posts 1 to 2

Nicd

Nicd

The Money.div function returns a tuple of {:ok, value} or {:error, reason}. So where you are using the function, you need to pick the value out of the tuple. Jason won’t encode tuples as JSON by default, so that’s what the error comes from.

So in your changeset function you should be doing maybe something like:

with {:ok, cost} <- Money.div(budget, term) do
  put_change(changeset, :anual_cost, cost)
else
  {:error,  reason} ->
    put_error(changeset, ... something something)
end

Btw annual is written with two n’s. :slight_smile:

hudsonbay

hudsonbay OP

HI, thank you very much. You are so right. I told you I was a newbie :slight_smile:

Also thanks for the typo clarification in annual :ok_hand: It made a lot of sense in Spanish to me :sweat_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews