arc icon indicating copy to clipboard operation
arc copied to clipboard

Attachment not uploaded to S3 on create, only on update

Open dbishai opened this issue 5 years ago • 3 comments

Environment

  • Elixir version (elixir -v): 1.8.2
  • Arc version (mix deps): 0.11
  • Arc dependencies when applicable (mix deps):
  • Operating system: Ubuntu 18.04

Expected behavior

Attachment will be uploaded to S3 when object is created with an empty changeset and set of attributes.

Actual behavior

Attachment is not uploaded until object is updated.

As stated above, if I create a new record with an attachment, the attachment is not uploaded to S3 until I update the record.

This works:

    case Posts.create_item(item_params) do
      {:ok, %Item{} = item} ->
        # hack
        with {:ok, %Item{} = _item} <- Posts.update_item(item, item_params) do
           redirect(conn, to: Routes.item_path(conn, :index))
        end

This does not:

    case Posts.create_item(item_params) do
      {:ok, %Item{} = item} ->

         redirect(conn, to: Routes.item_path(conn, :index))

posts.ex

  def create_item(attrs \\ %{}) do
    %Item{}
    |> Item.changeset(attrs)
    |> Repo.insert()
  end

  def update_item(%Item{} = item, attrs) do
    Item.changeset(item, attrs)
    |> Repo.update()
  end

dbishai avatar Jun 29 '19 03:06 dbishai

Is the attachment part of the Post schema? If so, I think you have to create the Post first so that it has an :id. WIthout an :id field, the attachment name generation will not work.

E.g., if the Post is going to have an id == 4, but it has not been created yet, instead of an attachment 4_myimage.png you will get _myimage.png.

(I think this is correct. Still new to it myself.)

sensiblearts avatar Jun 30 '19 16:06 sensiblearts

Well it's an attachment on the Item schema which belongs to the Post context, but yes. I assumed it would create the record first, then generate the name and upload using some after_create hook. It's looking like this is not the case.

dbishai avatar Jul 01 '19 15:07 dbishai

@dbishai I assume this issue is related to arc_ecto and not arc itself?

https://www.reddit.com/r/elixir/comments/5yo4ui/ecto_lack_of_callbacks_the_pain/

eprothro avatar Jul 15 '19 15:07 eprothro