arc icon indicating copy to clipboard operation
arc copied to clipboard

Scope is nil in nested models

Open maxgronlund opened this issue 8 years ago • 7 comments

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

maxgronlund avatar Mar 30 '16 06:03 maxgronlund

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

stavro avatar Mar 30 '16 17:03 stavro

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 avatar Mar 31 '16 07:03 maxgronlund

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

maxgronlund avatar Mar 31 '16 15:03 maxgronlund

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

maxgronlund avatar Mar 31 '16 15:03 maxgronlund

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

maxgronlund avatar Mar 31 '16 15:03 maxgronlund

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

maxgronlund avatar Apr 02 '16 15:04 maxgronlund

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.

jcrqr avatar Jan 02 '18 17:01 jcrqr