Why do we have Ecto.build_assoc?

If Ecto.build_assoc(post, :comments, body: "Excellent!") is equivalent to %Comment{post_id: post.id, body: "Excellent!"}, why do we have build_assoc in Ecto? I found it sometimes confusing to use.

5 Likes

Those are equivalent, yes. The goal of build_assoc is to allow you to work on the association names instead of the key names. That’s it.

8 Likes