Hi,
I’m trying to create a form with file field to upload a file. In order to store file into the proper folder I need to know resourse’s id in advance. So, I’ve split the creation transaction to two separate change-sets: changeset for the form fields and audio_changeset to upload the file.
When I call audio_changeset to update changeset and upload the file with invalid data, it changes the form state to update which changes form rendering as put action. This all happen inside the creating resource transaction. To overcome this and reset form status back to creating resource state I need to reset changeset.data.__meta__.state to nil.
It works but I think it has a better and prettier solution ![]()
def create_recording(attrs \\ %{}) do
Repo.transaction(fn ->
%Recording{}
|> Recording.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, recording} ->
Recording.audio_changeset(recording, attrs)
|> Repo.update()
|> case do
{:ok, recording_with_audio} -> recording_with_audio
{:error, changeset} ->
# make the form to a "new" state insted of "update"
meta = Map.put(changeset.data.__meta__, :state, nil)
data = Map.put(changeset.data, :__meta__, meta)
changeset
|> Map.put(:data, data)
|> Repo.rollback()
end
{:error, error} -> Repo.rollback(error)
end
end)
end






















