TIL: Elixir Ecto put_assoc vs cast_assoc
TLDR; If the thing exists, use put_assoc. If you’re creating it, use cast_assoc.
Ecto has two nifty methods for working with associated records in a changeset.
put_assoc/4 is used when you already have an associated struct and you want to assign the association in your changset.
cast_assoc/3 is used when you want to create the associated record along with your changeset. This might be what you are looking for if you are coming from Rails and looking for ‘accepts_nested_attributes_for’
By default cast_assoc/3 will use changeset/2 in your associated model, but you can specify a different by passing the :with option.