Ecto 变更集中的空原子

Empty atom in Ecto changeset

为什么在 Ecto changeset 方法中将参数设置为默认的 :empty 原子?例如

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

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

这在 Programming Phoenix 中有解释:

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