Working with Associations in Queries
Now that we’ve got some associations defined, let’s put them to work. Go to the music_db project and open up a mix session with iex -S mix.
First grab the record for the album Kind Of Blue:
| album = Repo.get_by(Album, title: "Kind Of Blue") |
Our gut tells us that if we want to see the tracks for this album, we would just do this:
| album.tracks |
But our gut is wrong. Instead, we get this:
| #Ecto.Association.NotLoaded<association :tracks is not loaded> |
This is not an error. It’s a placeholder value indicating that the tracks records associated with this album have not yet been retrieved from the database.
You might well wonder why Ecto doesn’t just load the ...
Get Programming Ecto now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.