Hey! Can you elaborate more on what you mean by “I’d like nil values not to be saved in the database”? It seems a bit contradictory since the concept of nil actually represents the absence of a value.
if you’re not super keen on creating your own custom type, there’s nothing stopping you from defining a changeset function in PersonData and relying on it to handle validations/overwrites.
e.g.,
# in person.ex
defmodule App.Person do
schema "people" do
embeds_one :data, App.CustomType
end
def changeset(person, attrs) do
person
|> cast_embed(
:data,
with: &App.PersonData.changeset/2,
)
end
end
# in person_data.ex
defmodule App.PersonData do
embedded_schema do
field :name, :string
field :slug, :string
end
def changeset(person_data, attrs) do
person_data
|> cast(attrs, [:name, :slug])
|> remove_nil_values(...)
end
def remove_nil_values(changeset, ...) do
...
end
end
yes! this is what i’m looking to do how would the remove_nil_values function look? it seems the App.PersonData struct always adds null in the db for empty values