Casting to Money.Ecto.Composite.Type in changeset

Has anyone here had luck casting a Money.Ecto.Composite.Type in a changeset?

schema "orders" do
  field(:price, Money.Ecto.Composite.Type, default: Money.new(0, "USD"))
end

def changeset(order, attrs \\ %{}) do
  order
  |> cast(attrs, [:price])
end

attrs contains

%{"amount" => 10, "currency" => "USD"}

For the life of me, I cannot seem to get this to cast as expected. price is never set in the changeset.

As soon as I asked, I realized attrs should be %{"price" => %{"amount" => 10, "currency" => "USD"}}

All sorted

2 Likes