gpt4 book ai didi

Elixir Ecto : Could someone give an example of Ecto. Multi.run/5

转载 作者:行者123 更新时间:2023-12-04 22:43:00 25 4
gpt4 key购买 nike

docs状态

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

Similar to run/3, but allows to pass module name, function and arguments. The function should return either {:ok, value} or {:error, value}, and will receive changes so far as the first argument (prepened to those passed in the call to the function).



但我不确定如何使用它。假设我想在 Ecto.Multi 中运行这个函数:
def some_fun(value, other_value) do
case value do
nil -> {:error, other_value}
_ -> {:ok, other_value}
end
end

那将如何运作?

最佳答案

我假设你想要 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)

关于Elixir Ecto : Could someone give an example of Ecto. Multi.run/5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40746936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com