Currently working on Phoenix project using Ecto + Postgres. When creating a comment, as comment belongs_to
both User and Article, is there a way to build multiple associations to generate one changeset?
Something like this pseudocode
comment_changeset = build_assoc(article, :comment)
|> build_assoc(user, :comment)
Any ideas?
As Justin mentioned, you can use put_assoc
to do that, so off the top of my head I think something like this should work.
comment_changeset =
article
|> Ecto.build_assoc(:comment)
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:user, user)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With