12
votes

Why in an Ecto changeset method do you set the params to the default :empty atom? e.g.

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

Does this allow you to call the changeset method with nil for params?

1

1 Answers

18
votes

This is explained in 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.

So :empty is used as a placeholder so that we can distinguish between a blank form submission and no parameters specified.


As Stefan notes in the comment below:

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