Understanding the gotchas of associations

Cool! We now have our Poll, representing a row in the database with an ID of 1. Let's try to access the options on that poll:

iex(12)> poll.options#Ecto.Association.NotLoaded<association :options is not loaded>

This seems strange. We queried for the Poll and got that back, so why didn't it load our Options association with it? Well, this is actually intentional behavior for Ecto; it's designed to not lazy-load data from the database. Anyone who has ever worked with a lazy loading relational model can tell you just what sorts of problems it tends to introduce over time concerning the performance and maintenance of an application, so to avoid that problem Ecto just straight up doesn't lazy load any ...

Get Phoenix Web Development 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.