我想用物化路径构建一些祖先功能。
所以当我创建第一个项目时(它可能会得到1的id)。 ancestry
项目为1/
。然后,当我创建一个属于该项的子项时,它将ancestry
1/2/
或parent_ancestry + :id + /
,但我无法访问变更集中的:id,是否存在有什么方法可以达到这个目的吗?
我期待:
changeset = Model.changeset(%Model{}, %{thing: "thing"})
{:ok, model} = Repo.insert(changeset)
%Model{id: 1, thing: "thing", ancestry: "1/"}
changeset2 = Model.changeset(%Model{}, %{thing: "thing2", parent_id: 1})
{:ok, model} = Repo.insert(changeset2)
%Model{id: 2, thing: "thing2", ancestry: "1/2/"} # I know how to grab the parent ancestry here, I know it's not just the parent_id. My issue is the current model's id