Skip to Main Content
Mastering Elixir
book

Mastering Elixir

by André Albuquerque, Daniel Caixinha
July 2018
Intermediate to advanced content levelIntermediate to advanced
574 pages
14h 7m
English
Packt Publishing
Content preview from Mastering Elixir

Loading schemas and their associations

By default, Ecto doesn't load any associations for us. If we retrieve an Owner schema, Ecto will tell you that the files field isn't loaded yet:

iex> owner = Repo.get!(Owner, "13KvtrMQ9uRB8MqkJpKSSQZxnBa")11:15:18.878 [debug] QUERY OK source="users" db=1.2ms decode=2.8msSELECT u0."id", u0."email" FROM "users" AS u0 WHERE (u0."id" = $1) ["13KvtrMQ9uRB8MqkJpKSSQZxnBa"]%ElixirDrip.Storage.Owner{  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,  email: "jose@hey.ho",  files: #Ecto.Association.NotLoaded<association :files is not loaded>,  id: "13KvtrMQ9uRB8MqkJpKSSQZxnBa"}

If you want to access their files, you need to explicitly call the Repo.preload/2 callback with the schema and the field to load:

iex> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Testing Elixir

Testing Elixir

Andrea Leopardi, Jeffrey Matthias

Publisher Resources

ISBN: 9781788472678Supplemental Content