arc icon indicating copy to clipboard operation
arc copied to clipboard

scope.id nil, upload not nested.

Open codeithuman opened this issue 9 years ago • 3 comments

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}.

codeithuman avatar May 19 '16 02:05 codeithuman

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

codeithuman avatar May 19 '16 18:05 codeithuman

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:

  1. Get a value
  2. Call a cast method with type, value params
  3. With help of Arc.Ecto.Type it downloads a pic
  4. Then Uploader tries to copy a file to path from storage_dir which 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 avatar Feb 27 '17 11:02 mojidabckuu

@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.

joskar avatar Sep 06 '18 07:09 joskar