在我的Tag模型代码中
schema "tags" do
field :name, :string
field :parent, :integer # parent tag id
timestamps
end
def add_error_when_not_exists_tag_id(changeset, params) do
tags = Repo.all(Tag)
is_exists_tag_id = Enum.reduce(tags, fn(x, acc) -> acc || (x.id === params.parent) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "not exists parent!")
end
Run Code Online (Sandbox Code Playgroud)
上面的代码导致下面的错误.
(UndefinedFunctionError) undefined function: Repo.all/1 (module Repo is not available)
Run Code Online (Sandbox Code Playgroud)
我可以修复错误吗?
Tag模型是嵌套标签模型.
标签可以有父标签.
最终代码如下.这很好.
在模型中
def add_error_when_not_exists_tag_id(changeset, params, tags) do
is_exists_tag_id = Enum.reduce(tags, false, fn(x, acc) -> acc || (Integer.to_string(x.id) …Run Code Online (Sandbox Code Playgroud)