April 2019
Beginner to intermediate
244 pages
6h 34m
English
Now let’s set up the album schema that will use these embeds. This will be a regular schema, similar to what we’ve worked with earlier. We’ll need a new database table to go with it, as we’re storing our child records with embeds rather than associations.
Here’s what our new migration looks like:
| | def change do |
| | create table("albums_with_embeds") do |
| | add(:title, :string) |
| | add(:artist, :jsonb) |
| | add(:tracks, {:array, :jsonb}, default: []) |
| | end |
| | end |
The Ecto types for embeds are :map (for has-one relationships) and {:array, :map} (for has-many). At the database level, we use json (or possibly ...
Read now
Unlock full access