April 2019
Beginner to intermediate
244 pages
6h 34m
English
To save the data from these schemas into the artists table, we have a couple of options. One way would be to add extra changeset functions to our Artist schema that take a Band or SoloArtist struct as input:
| | 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} |
Read now
Unlock full access