I upload a file in the changeset function like so:
schema "questions" do
field :media, :string
field :text, :string
timestamps(type: :utc_datetime)
end
def changeset(question, attrs) do
question
|> cast(attrs, [:text])
|> validate_required([:text])
|> add_media_path(attrs["media"])
end
# will upload the file in the right directory
# and set the path in the :media field
defp add_media_path(changeset, %Plug.Upload{} = upload) do
put_change(changeset, :media, Uploader.upload(upload))
end
defp add_media_path(changeset, _), do: changeset
Is this code wrong? If so, why?
I think I’ll have a problem as soon as I want to make the media field required?
Another possibility is to upload the file from the context:
def create_question(attrs \\ %{}) do
%Question{}
|> Question.changeset(attrs)
# TODO: upload file if changeset is valid
|> Repo.insert()
end
But then how can I upload the file only if the changeset is valid?