如何使用 Ecto 更新多对多关联
How to update a many-to-many association with Ecto
我有一个 Phoenix 应用程序,其中包含两个具有多对多关系的资源。为简化起见,我们称它们为 post 和标签。当我从头开始创建 post 时,我可以将现有标签与其相关联。我还可以编辑一个没有标签的 post 来关联标签。但是,每当我想编辑与 post 关联的标签时,我都会收到此错误:
you are attempting to change relation :tags of MyApp.Posts.Post but the `:on_replace` option of
this relation is set to `:raise`
这是我的变更集 - 我使用的 put_assoc
有 4 个参数,但根据文档,未使用 opts 一个,所以我无法设置 on_replace 选项:
def changeset(%__MODULE__{} = user, attrs) do
tag_ids = if attrs["tags"], do: attrs["tags"], else: []
tags = Tags.by_ids(tag_ids)
user
|> Repo.preload(:tags)
|> cast(attrs, [:title, :body])
|> put_assoc(:tags, tags)
|> validate_required([:body])
end
我的想法是只更新两者之间的关联 - 我永远不会从 posts 表单中创建或删除标签本身。我应该以不同的方式更新它们吗?
我有一个 Phoenix 应用程序,其中包含两个具有多对多关系的资源。为简化起见,我们称它们为 post 和标签。当我从头开始创建 post 时,我可以将现有标签与其相关联。我还可以编辑一个没有标签的 post 来关联标签。但是,每当我想编辑与 post 关联的标签时,我都会收到此错误:
you are attempting to change relation :tags of MyApp.Posts.Post but the `:on_replace` option of
this relation is set to `:raise`
这是我的变更集 - 我使用的 put_assoc
有 4 个参数,但根据文档,未使用 opts 一个,所以我无法设置 on_replace 选项:
def changeset(%__MODULE__{} = user, attrs) do
tag_ids = if attrs["tags"], do: attrs["tags"], else: []
tags = Tags.by_ids(tag_ids)
user
|> Repo.preload(:tags)
|> cast(attrs, [:title, :body])
|> put_assoc(:tags, tags)
|> validate_required([:body])
end
我的想法是只更新两者之间的关联 - 我永远不会从 posts 表单中创建或删除标签本身。我应该以不同的方式更新它们吗?