stavro / arc

:paperclip: Flexible file upload and attachment library for Elixir
1.16k stars 210 forks source link

Scope is nil in nested models #58

Open maxgronlund opened 8 years ago

maxgronlund commented 8 years ago

Thanks for a great uploader. I ran into an issue when attaching files to nested model

I have modified the uploader so I can upload an image on creation

def storage_dir(version, {file, scope}) do "uploads/banner/images/#{scope.storage_dir}" <<< storage_id is a UUID end

This works great :-)

Bui If I use the same uploader on a nested model e.g a user has many party_images then scope is nil when I try to create / update a `party_image, version and file is OK

stavro commented 8 years ago

Interesting! Are you saving the nested models via Ecto's cast_assoc?

maxgronlund commented 8 years ago

No I'm using build_assoc E.G. topic has many posts. Simplified snippet from the post_controller#create

changeset =
      topic
      |> build_assoc(:posts)
      |> Post.changeset(post_params)
    Repo.insert(changeset)

and post_controller#update

post      = Repo.get!(Post, id)
changeset = Post.changeset(post, post_params)
Repo.update(changeset)
maxgronlund commented 8 years ago

My uploader. works great with models that don't belongs to some other model image_uploader.ex.zip

maxgronlund commented 8 years ago

The model that belongs to a topic post.ex.zip

maxgronlund commented 8 years ago

The controller where I create and update the post post_controller.ex.zip

maxgronlund commented 8 years ago

I can make a workaround in the controller like this

post_params = Map.merge( post_params, %{ "user_id" => conn.assigns.current_user.id, "views" => 0, "topic_id" => topic.id })

jcrqr commented 6 years ago

I've a case where the user can upload multiple attachments to an event. How could I workaround this issue?

I'm using put_assoc/3 to create the association.

Thanks in advance.