Actually, we can do it much better because cast_assoc do all the heavy work for us.
In my dummy project I changed the following:
file: detail.ex
change: deleted the invoice_id
code:
def changeset(%Detail{} = detail, attrs) do
detail
|> cast(attrs, [:tax, :code, :desc, :up])
|> validate_required([:tax, :code, :desc, :up])
end
file: invoice.ex
change: added cast_assoc function
code:
def changeset(%Invoice{} = invoice, attrs) do
invoice
|> cast(attrs, [:provider])
|> validate_required([:provider])
|> cast_assoc(:details)
end
documents.ex
change: a) deleted functions: add_records, save_invoice; b) changed the create_invoice function
code:
def create_invoice(attrs \\ %{}) do
%Invoice{}
|> Invoice.changeset(attrs)
|> Repo.insert()
end
I will work in a second version of this post to clarify.
Hope this works,
Best regards,