【发布时间】:2016-01-22 11:21:23
【问题描述】:
在参数中通过parent_id 将给定child 的关联从parent_a 更改为parent_b 会留下陈旧的record.parent 对象。
例如(假设参数匹配%{child: %{id: '1', parent_id: '6'}})
# ...
child = Repo.get(Child, child_id)
|> preload([:parent])
changeset = Child.changeset(child, child_params)
case Repo.update(changeset) do
{:ok, child} ->
IO.puts child.parent_id # returns '6', or the new, changed `id`
IO.puts child.parent.id # returns '5', or the old id
# child.parent is stale
# ...
更新后检索新关联的父记录的正确方法是什么?
【问题讨论】:
标签: elixir phoenix-framework ecto