ECTO建立多个协会 [英] Ecto build multiple assoc
本文介绍了ECTO建立多个协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前正在使用Ecto + Postgres进行Phoenix项目.创建注释时,作为注释属于用户和文章都属于
to
,有没有一种方法可以建立多个关联来生成一个变更集?
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?
类似于此伪代码
comment_changeset = build_assoc(article, :comment)
|> build_assoc(user, :comment)
有什么想法吗?
推荐答案
就像贾斯汀提到的那样,您可以使用 put_assoc
来做到这一点,因此,我想起来应该是这样的
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)
这篇关于ECTO建立多个协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文