gpt4 book ai didi

elixir - Ecto 变更集中的空原子

转载 作者:行者123 更新时间:2023-12-04 00:46:42 27 4
gpt4 key购买 nike

为什么在 Ecto changeset方法您是否将参数设置为默认值:empty原子?例如

def changeset(user, params \\ :empty) do
...

这是否允许您使用 nil 为参数调用变更集方法?

最佳答案

这在凤凰编程中有解释:

Chris says: If there are no parameters specified, we can’t just default to an empty map because that would be indistinguishable from a blank form submission. Instead, we default params to the atom :empty. By convention, Ecto will produce an invalid changeset, with empty parameters.



所以 :empty用作占位符,以便我们可以区分空白表单提交和未指定参数。

Stefan以下评论中的注释:

Note that with Ecto 2.0 it uses an empty map: def changeset(user, params \\ %{}) do

关于elixir - Ecto 变更集中的空原子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570817/

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