Save the Table-less Structs

Learn how to save the table-less structs.

Save data from schemas

We have a couple of options to save the data from these schemas into the artists table. One way would be to add extra changeset functions to our Artist schema that take a Band or SoloArtist struct as input.

Press + to interact
def changeset(%MusicDB.Band{} = band) do
{:ok, birth_date} = Date.new(band.year_started, 1, 1)
{:ok, death_date} = Date.new(band.year_ended, 12, 31)
changeset(%Artist{
name: band.name,
birth_date: birth_date,
death_date: death_date
}, %{})
end
def changeset(%MusicDB.SoloArtist{} = solo_artist) do
name =
"#{solo_artist.name1} #{solo_artist.name2} #{solo_artist.name3}"
|> String.trim()
changeset(%Artist{
name: name,
birth_date: solo_artist.birth_date,
death_date: solo_artist.death_date
}, %{})
end

With ...