Ecto.build_assoc

You're seeing just the function build_assoc, go back to Ecto module for more information.
Link to this function

build_assoc(struct, assoc, attributes \\ %{})

View Source

Builds a struct from the given assoc in struct.

Examples

If the relationship is a has_one or has_many and the primary key is set in the parent struct, the key will automatically be set in the built association:

iex> post = Repo.get(Post, 13)
%Post{id: 13}
iex> build_assoc(post, :comments)
%Comment{id: nil, post_id: 13}

Note though it doesn't happen with belongs_to cases, as the key is often the primary key and such is usually generated dynamically:

iex> comment = Repo.get(Comment, 13)
%Comment{id: 13, post_id: 25}
iex> build_assoc(comment, :post)
%Post{id: nil}

You can also pass the attributes, which can be a map or a keyword list, to set the struct's fields except the association key.

iex> build_assoc(post, :comments, text: "cool")
%Comment{id: nil, post_id: 13, text: "cool"}

iex> build_assoc(post, :comments, %{text: "cool"})
%Comment{id: nil, post_id: 13, text: "cool"}

iex> build_assoc(post, :comments, post_id: 1)
%Comment{id: nil, post_id: 13}

The given attributes are expected to be structured data. If you want to build an association with external data, such as a request parameters, you can use Ecto.Changeset.cast/3 after build_assoc/3:

parent
|> Ecto.build_assoc(:child)
|> Ecto.Changeset.cast(params, [:field1, :field2])