scope.id nil, upload not nested.
When creating a new user, I can upload a profile image, but since the user is being created the same time as the image is being uploaded, my scope.id is nil. Do I need to use an uuid as the user.id? Is there something else I'm missing?
def storage_dir(version, {file, scope}) do
"uploads/user/profile_photo/#{scope.id}"
end
Because scope.id is nil, all my uploads are being placed in the uploads/user/profile_photo/ instead of uploads/user/profile_photo/#{user.id}.
I was able to work around this by hashing the user's email and using that instead of scope.id.
def storage_dir(version, {file, scope}) do
"uploads/user/profile_photo/user_#{uniq_id(scope.email)}"
end
defp uniq_id(string) do
:crypto.hash(:md5, string) |> Base.encode16
end
I faced with the same issue. I spent 2hrs to go deep into stacktrace how it is transformed from a string value which is a URL to string. The scenario for the changeset is:
- Get a value
- Call a cast method with type, value params
- With help of
Arc.Ecto.Typeit downloads a pic - Then Uploader tries to copy a file to path from
storage_dirwhich receives scope as nil value. So I think the approach that could fit here is to create a user first and then update it with a file name. It is a gross workaround because user creation should be transactional.
@mojidabckuu @codeithuman I'm facing the same problem but in my scenario I can't do something like you proposed. I believe splitting the insert query to multiple steps using Ecto.Multi functionality will be the most appropriate solution here.