Elixir Ecto: Can anyone give an example of Ecto.Multi.run/5

docs condition

run(t, name, module, function, args) :: t when function: atom, args: [any]

Similarly run/3, but allows you to pass the module name, function and arguments. The function must return either or {:ok, value}, {:error, value}and will receive changes until the first argument (until then, which were passed when the function was called).

But I'm not sure how to use this. Let's say I have this function that I want to run inside Ecto.Multi:

def some_fun(value, other_value) do
  case value do
    nil -> {:error, other_value}
    _ -> {:ok, other_value}
  end
end

How it works?

+4
source share
1 answer

, value " ", other_value - , Multi.run/5. , Foo:

defmodule Foo do
  def some_fun(value, other_value) do
    case value do
      nil -> {:error, other_value}
      _ -> {:ok, other_value}
    end
  end
end

Multi.run/5 :

Multi.run(multi, name, Foo, :some_fun, [other_value])

Multi.run/3:

Multi.run(multi, name, fn value -> Foo.some_fun(value, other_value) end)
+10

Source: https://habr.com/ru/post/1661606/


All Articles