Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty atom in Ecto changeset

Tags:

elixir

ecto

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?

like image 306
Elliot Larson Avatar asked Jan 02 '16 21:01

Elliot Larson


1 Answers

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

like image 131
Ryan Bigg Avatar answered Oct 23 '22 00:10

Ryan Bigg