Save the Table-less Structs
Learn how to save the table-less structs.
We'll cover the following...
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}, %{})enddef changeset(%MusicDB.SoloArtist{} = solo_artist) doname ="#{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 ...