SebAlbert

SebAlbert

Programming Ecto: Question on cast |> cast_assoc (page 82)

While reading and following along Chapter 4, we use a map params containing a simple name string key of an Artist schema and a key containing a list with one map for the has_many assoc towards Album named albums.

I am confused by the following:

changeset =
  %Artist{}
  |> cast(params, [:name])
  |> cast_assoc(:albums)

How does the last call get the albums part out of the params map when it has been filtered out by the previous step? When leaving off the last element of that pipe, I get this:

#Ecto.Changeset<
  action: nil,
  changes: %{name: "Esperanza Spalding"},
  errors: [],
  data: #MusicDB.Artist<>,
  valid?: true
>

This does not seem to contain the unused key from the params map anywhere! But somewhat magically, when passing it on to cast_assoc(:albums), it suddenly knows its contents.

Maybe I haven’t grokked the console representation of stuff (that thing starting with #, the struct name, and then its contents enclosed in angle brackets), appearing in orange colour on my terminal. Is that providable by the struct implementation (much like a toString() in Java or __str__ or __repr__ in Python), and Changesets do not include some things they hold on to behind the scenes?

Where Next?

Popular Pragmatic Bookshelf topics Top

GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
New

Other popular topics Top

AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Sub Categories: