Thoughts on multi operation naming

Hey there,

I have been using https://hexdocs.pm/ecto/Ecto.Multi.html for a while now, and I was wondering what kind of convention do other people follow when they are naming the operations, especially when it is a Multi in an Enum.reduce/3.
With a regular Multi I usually use an atom as the examples do.

For a Multi in an reduce such as:

Enum.reduce(user_ids, Multi.new(), fn user_id, multi ->
      blocked_user = Accounts.get_user_by_hash_id(user_id)

      changeset = Block.changeset(%Block{}, user, blocked_user)

      multi
      |> Multi.insert("user_block_#{user.id}_#{blocked_user.id}", changeset)
    end)
    |> Repo.transaction()

I mostly use a string based on the structs I use, but this relies on both of them having an id and found.
Sometimes I use a uuid appended after the “user_block” for example, and in the docs I saw I could use a tuple.

How about you?

I mostly use a string based on the structs I use, but this relies on both of them having an id and found.
Sometimes I use a uuid appended after the “user_block” for example, and in the docs I saw I could use a tuple.

I do the same as you. It works fine IMO. But perhaps there is a better way? :slight_smile:

1 Like

You can use any term for naming you multi steps, not just atoms/strings. When mapping/reducing over lists I usually use tuples like {:products, product.id} or something like that.

2 Likes

I will start doing that going forward. Will make it much easier to pattern match on errors. Thanks for highlighting that.

1 Like

I also use tuples. When you don’t know the ID beforehand you can just use a counter. See Enum.with_index.

When inserting I use tuples like {:insert, :product, index}.

2 Likes