Ecto: empty_values docs clarification

Created on 25 Aug 2016  路  1Comment  路  Source: elixir-ecto/ecto

Precheck

  • Do not use the issues tracker for help or support requests (try Stack Overflow, IRC or mailing lists, etc).

    • This might be considered a support request - sorry if that's how it reads. I talked to some guys on IRC who suggested opening an issue after we couldn't figure out the best way to set empty_values

      Environment

  • Elixir version (elixir -v): 1.3.2

  • Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): PostgreSQL 9.5
  • Ecto version (mix deps): 2.0.4

    Current behavior

In my opinion, docs aren't clear on the best way to override empty_values. After getting help on IRC, I am doing

  @empty_values ["blank"]

  def changeset(struct, params \\ %{}) do
    struct
    |> change(%{})
    |> Map.put(:empty_values, @empty_values)
    |> cast(params, [:foo, :bar])
  end

I am still very new to Elixir and Ecto so my exposure to code is limited but this instantiating a changeset with change and then modifying the changeset's field with put all before calling cast felt a little unnatural to me.

Expected behavior

When I first asked for help a couple people thought the changeset would read an @empty_values module attribute from my model, which ended up not being the case. After some discussion, we settled on the code above. It works but it took us a while to reach that and we weren't confident that was the best approach. It would be helpful if the docs included the recommended way to set empty values here https://hexdocs.pm/ecto/Ecto.Changeset.html#module-empty-values

I am too new to this ecosystem to have an informed opinion on API design but as a new user, I kind of expected Ecto.Changeset to provide a function that would update the empty values.

If the above code is the best way to do set new empty values, I'd be happy to open a PR updating the documentation.

Thanks

Thanks for the work and time you've put into this project! I think empty values will be helpful once I figure out how to best use the feature. I love working with ecto 馃槃

Most helpful comment

Yes, we plan to provide an official API but it first requires us to deprecate the existing cast/4 API. It is coming soon, so please hang on tight. :)

>All comments

Yes, we plan to provide an official API but it first requires us to deprecate the existing cast/4 API. It is coming soon, so please hang on tight. :)

Was this page helpful?
0 / 5 - 0 ratings